I am writing a class which involves multiple methods for certain purpose. But however the final result of each method is saved in an instance variable so that the other methods can access them. In ruby however method call however returns the last value and this approach can also adopted. But I am confused as which approach is programmatically right and more ruby like.
An example for my code would be like this
Approach1:- Method call
class Foo
def cat
"This is a cat"
end
def bigcat
cat
end
end
mycat = Foo.new
mycat.cat
mycat.bigcat
Output:- This is a cat
This is a cat
Approach2:- Instance variable
class Foo
def cat
@cat_talk = "This is a cat"
end
def bigcat
@cat_talk
end
end
mycat = Foo.new
mycat.cat
mycat.bigcat
Output:- This is a cat
This is a cat
The above example may seem simple but in my ruby class the implementation is much more complex and larger.