by jablan · 14y ago · 0 comments
def foo s
s.concat 'foo'
end
a = 'bar'
foo a
a
# => "barfoo" class Fixnum
alias :realplus :+
def + b
@a ||= 0
@a = @a.realplus(b)
self.realplus(@a)
end
end
The integer value itself is immutable, though.Of course all hell will break loose and adding instance variables in certain classes like Fixnum will be extra slow (as if there aren't enough other reasons not to), since MRI uses type-tagging for Fixnum, Symbol and a few others, and so since they're not "real" objects, if you do anything that requires one, it creates one that's stored in a hash table.
def foo s
s = 'foo'
end
a = 'bar'
foo a
puts a
# => bar
The assignment in the method doesn't change the object you "passed" in. Rather than passing by reference, think of Ruby as passing the value of a reference.For more examples and explanation, see http://khelll.com/blog/ruby/ruby-pass-by-value-or-by-referen...
They both do "pass reference by value". Which is a big difference.