Ruby Object Prints Out as Pointer

Ruby object prints out as pointer

When you use new method, you get 'reference' on newly created object. puts kernel method returns some internal ruby information about this object. If you want to get any information about state your object, you can use getter method:

class Adder
def initialize(my_num)
@my_num = my_num
end
def my_num
@my_num
end
end
y = Adder.new(12)
puts y.my_num # => 12

Or you can use 'attr_reader' method that define a couple of setter and getter methods behind the scene:

class Adder
attr_accessor :my_num

def initialize(my_num)
@my_num = my_num
end
end
y = Adder.new(12)
puts y.my_num # => 12

How to print the memory location of a Ruby object

I don't know why would you want such a feature, and this is very implementation-specific, but on MRI 1.9 you can (ab)use object_id:

ruby-1.9.2-p180 :022 > class A; end
=> nil
ruby-1.9.2-p180 :023 > a = A.new
=> #<A:0xa2b72e4>
ruby-1.9.2-p180 :024 > a.object_id.to_s(16)
=> "515b972"
ruby-1.9.2-p180 :025 > (a.object_id << 1).to_s(16)
=> "a2b72e4"

For an explanation of why does it work, check out the relevant lines in MRI gc.c.

Note that this will not work on other implementations (JRuby, Rubinius), and may very well break in future versions of Ruby. Also, there may be no way at all to get the address in some implementations (I suspect that you cannot in JRuby; I'm not sure, through).

Object assignment and pointers

There are a lot of questions in this question. The main thing to know is assignment never makes a copy in ruby, but methods often return new objects instead of modifying existing objects. For immutable objects like Fixnums, you can ignore this, but for objects like arrays or Foo instances, to make a copy you must do bar.dup.

As for the array example, foo += is not concatenating onto the array stored in foo, to do that you would do foo.concat(['a']). Instead, it is making a new array and assigning foo to that. The documentation for the Array class mentions which methods mutate the array in place and which return a new array.

Print memory address for Ruby array

Use the method Object#object_id.

Returns an integer identifier for obj. The same number will be returned on all calls to id for a given object, and no two active objects will share an id. #object_id is a different concept from the :name notation, which returns the symbol id of name.

Example :-

Arup-iMac:arup_ruby $ irb
2.1.2 :001 > s = "I am a string"
=> "I am a string"
2.1.2 :002 > obj_id = s.object_id
=> 2156122060
2.1.2 :003 > ObjectSpace._id2ref obj_id
=> "I am a string"
2.1.2 :004 >

In Ruby, why does inspect() print out some kind of object id which is different from what object_id() gives?

The default implementation of inspect calls the default implementation of to_s, which just shows the hexadecimal value of the object directly, as seen in the Object#to_s docs (click on the method description to reveal the source).

Meanwhile the comments in the C source underlying the implementation of object_id shows that there are different “namespaces” for Ruby values and object ids, depending on the type of the object (e.g. the lowest bit seems to be zero for all but Fixnums). You can see that in Object#object_id docs (click to reveal the source).

From there we can see that in the “object id space” (returned by object_id) the ids of objects start from the second bit on the right (with the first bit being zero), but in “value space” (used by inspect) they start from the third bit on the right (with the first two bits zero). So, to convert the values from the “object id space” to the “value space”, we can shift the object_id to the left by one bit and get the same result that is shown by inspect:

> '%x' % (36971870 << 1)
=> "4684abc"

> a = Foo.new
=> #<Foo:0x5cfe4>
> '%x' % (a.object_id << 1)
=> "5cfe4"

How to return value from a Ruby function for use in the rest of the program

I recommend you read more about why Ruby is 'pass-by-value'. In mrn = 12, mrn is a variable, a pointer that points to an object which is 12 in this case.

When you're passing mrn to mrn_value, you're passing a COPY of that pointer. Now you have 2 pointers pointing to the same object. The moment you do mrn = '00000' + mrn you're telling the second pointer to no longer point to 2 but to whatever else is on the right side. Your original mrn pointer outside of the mrn_value method still points to 2, as you can see.

You can read more on Posts on StackOverflow on why Ruby is 'pass by value'. To give you a simplified version to demonstrate:

a = 1

def will_a_be_changed(a)
a = 2
puts a #=> 2
end

will_a_be_changed(a)
puts a #=> 1


Related Topics



Leave a reply



Submit