Category talk:Ruby: Difference between revisions

no edit summary
(Why I am reverting it to 'parampass=value'.)
No edit summary
Line 31:
 
Ruby would act like Perl if the Ruby program would use <tt>x.replace "different string"</tt>. This would work because <tt>x</tt> is a reference to a mutable string. I think that Ruby is pass by value because I can use assignment to change the value of <tt>x</tt>, to refer to some other string. I am reverting the page to 'parampass=value', after 74.215.210.198 reverted the page to 'parampass=reference', after I changed it to 'parampass=value'. --[[User:Kernigh|Kernigh]] 02:31, 16 March 2011 (UTC)
 
-----
 
Ruby passes by reference since it don't make copies of the objects when passing then to a method. Look:
 
<lang ruby>def f(x)
x.replace("different string")
x << " from method"
x = "another variable"
end
 
x = "original string"
f(x)
puts x # => "different string from method"</lang>
 
All variables are, internally, pointers to objects, so, when I pass the variable, it passes the reference to the object, that allow me to change directly the object. But I can't set the variable because when you set it inside the method you are creating another variable that have nothing to do with the old one. I'm reversing the change on "parampass", to 'reference'.
 
[[Special:Contributions/189.104.25.246|189.104.25.246]] 02:03, 10 July 2011 (UTC)
Anonymous user