Ruby - passing by value or by reference -


i've searched on this, , seems ruby passes value, simple little example subset of larger program, , i'm little confused.

a = "j123" b = a.slice!(0) puts puts b 

this displays

123 123 

and don't understand why value of "b" changing.

because both a , b pointers refer same object. aren't object itself. can pass pointers arguments in ruby, , pointers passed value.

this similar java, javascript , python, , unlike c++, c#, haskell , scala.

the difference of interest in following example:

def f(y)   y = 2 end x = 1 f(x) puts x # 1, not 2 

if x passed reference, print 2.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -