Friday, 22 March 2013

Call by reference in Ruby

In Ruby, some objects are passed by value and some by reference. In case, we want to pass by reference some object which by default gets passed by value, we can just use the id value of the object in Ruby's memory, i.e. the object space. The id can be obtained as follows.

some_object = "some intialization"

Here is how pass by reference would work using object id values.

def caller
    blah = "blahblueblah"

def callee(object_id)
    puts ObjectSpace._id2ref(doc.object_id)

Such use should be highly improbable and might even turn out to be bad practice; but sometimes we just want to do things for the heck of it.

No comments: