导航

RUBY TIME

Posted on 2011-02-24 22:49  blackwind  阅读(113)  评论(0)    收藏  举报

when uses the "reverse" method  without a expression like this
 
str="hello";
str.reverse;

no change will be happen,the value of str is still "hello".

but it will be reversed when uses the method"reverse!",string value changed like

str="hello";
str.reverse!;

so output value is "olleh".above ,str is still the same object_id.no new object_id is created


and there is another method to do the same as

str="hello";
str=str.reverse!;

as a result , there is a new object created for "str" during the process

the difference between operator "+" and "<<" is that string on the left of operator is changed when used operator "<<".let take a look at program as below.

stra = "a";
strb = "b";
strc = stra + strb

puts(stra);-----  a
puts(strb);-----  b
puts(strc);-----  ab


stra = "a";
strb = "b";
strc = "good bye"
strc = stra<<strb ;

puts(stra);-----  ab (you see the defference?)
puts(strb);-----  b
puts(strc);-----  ab

however, stra retains the same object_id , no object is created.
stra and strc have the same value,otherwise the object_id of strc is
now identical with that of stra.

===============================