look this class:
class Test
def initialize a, b, c
@a = a, @b = b, @c = c
end
end
class AnotherTest
def initialize a, b, c
@a = a
@b = b
@c = c
end
end
array = []
array.push Test.new "a1" ,"b1", "c1"
array.push AnotherTest.new "a2" ,"b2", "c2"
p array
I think this should be the same,but not:
<Test:0x000000022aba78 @b="b1", @c="c1", @a=["a1", "b1", "c1"]>
<AnotherTest:0x000000022ab9b0 @a="a2", @b="b2", @c="c2">]
Anybody who can give me an explain?
If you try in irb this expression:
As you can see, the assignment operation returns the result, because in ruby every expression should return something. So then this expression:
will return the value of
@b. Then in this expressionwhere
@b = band@c = cwill evaluate tobandcSo finally we will have this expression:
And as you know it’s another form for initialization of array
This code will work equivalently to yours:
Addition:
The order of evaluating is significant.
If you try this expression:
Firstly, it will evaluate everything in parentheses:
So then we’ll get this