Category talk:Ruby: Difference between revisions

Why I am reverting it to 'parampass=value'.
(Ruby is pass-by-value as you say....)
(Why I am reverting it to 'parampass=value'.)
Line 4:
 
: Ruby is pass-by-value as you say. I changed the page from 'parampass=reference' to 'parampass=value'. I also changed it from 'execution=bytecode' to 'execution=interpreted'. Ruby 1.9 compiles a program to bytecode and executes the bytecode, but this is an internal detail of the interpreter. There is no Java .class or Python .pyc file. --[[User:Kernigh|Kernigh]] 01:44, 9 March 2011 (UTC)
 
----
This program shows that Ruby passes by value:
 
<lang ruby># Ruby
def f(x)
x = "different string"
end
 
x = "original string"
f(x)
puts x # => "original string"</lang>
 
If Ruby would pass by reference, then this program would print "different string". But it prints "original string", because the two <tt>x</tt> have distinct values. Contrast [[Perl]], which passes by reference:
 
<lang perl># Perl
sub f {
$_[0] = "different string";
}
 
my $x = "original string";
f($x);
print "$x\n"; # => "different string"</lang>
 
This program prints "different string" because Perl passes <tt>$x</tt> by reference, so <tt>$_[0]</tt> is an alias of <tt>$x</tt>. (Many Perl subs use <tt>my $x = shift;</tt> to create a distinct value.)
 
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)
Anonymous user