x = Restaurant.new(bracket_method)
puts x.inspect
puts x.class
puts
y = Restaurant.new.chain_method
puts y.inspect
puts y.class
end
“bracket_method” is logical, but what is going on in case of
“chain_method” ?
Did I instantiate object “y” of class “Restaurant” without
initialization ?
How is that possible ?
The initialize method is called internally by “new”, passing all the
arguments you passed to “new”. In this case you are not passing any,
so initialize is called with no arguments. Now, if you look closely at
the definition of your initialize, you have an optional argument with
a default value, so when you don’t pass anything, “args” is an empty
hash, and so @name, @cuisine and @price are initialized to empty
values. When you then call “chain_method” on an object initialized
this way, those variables are assigned to the values “Pizza store” and
so on.
On Wed, Oct 23, 2013 at 12:59 PM, Stu P. D’naim [email protected]
wrote:
don’t get error, but when I remove args = {}, there it is. For a moment
y = rest.chain_method @name = “www store”
equal to strings from chain_method_two ?
It’s supposed to be a setter method
Methods in Ruby return the value of the last expression. In your case
the last expression is @wrongz = “www is printed”, which evaluates to
the String “www is printed”, so that’s what the method returns.
The initialize method is called internally by “new”, passing all the
arguments you passed to “new”. In this case you are not passing any,
so initialize is called with no arguments. Now, if you look closely at
the definition of your initialize, you have an optional argument with
a default value
ahh, I forgot about significance of default value, I was wondering why I
don’t get error, but when I remove args = {}, there it is. For a moment
I thought that by doing Class.new.chain_method I was overriding .new
Thanks !
Abinoam Jr. wrote in post #1125275:
y = Restaurant.new.chain_method
is the same as …
rest = Restaurant.new
y = rest.chain_method
‘y’ will be the same object as ‘rest’ because the chain_method is
returning self.
This explains a lot, thank you ! Now, there is only one little thing
that I still don’t understand …
if I add to my code something like this:
def chain_method_two @name = “www store” @cuisine = “www Food” @price = “5w” @wrongz = “www is printed”
end
w = Restaurant.new.chain_method_two
puts w.inspect
now I get new object (because I’m not returning self) but: @name, @couisine and @price are all equal to nil … shouldn’t they be
equal to strings from chain_method_two ?
It’s supposed to be a setter method
I know that, but I thought that by running .chain_method_two, instance
variables values should be changed from nil to newly assigned strings
… I hate it when I think I finally understood something, but it turns
out that it doesn’t work the way I think it will … I guess I’m gonna
go back and check out again stuff about reader/writer methods.
Thanks for your time !
On Wed, Oct 23, 2013 at 1:41 PM, Stu P. D’naim [email protected]
wrote:
I know that, but I thought that by running .chain_method_two, instance
variables values should be changed from nil to newly assigned strings
… I hate it when I think I finally understood something, but it turns
out that it doesn’t work the way I think it will … I guess I’m gonna
go back and check out again stuff about reader/writer methods.
Thanks for your time !
How are you checking that they are nil, because you lose the reference
to the object you modify:
w = Restaurant.new.chain_method_two
if you do this, Restaurant.new will return the instance, on which you
call chain_method_two, but you don’t keep a reference to the instance,
so how do you know the values were not changed?