1

I'm learning Ruby and made a class to help:

class WhatImDoing
    def initialize
        puts "not doing anything"
    end
end

with the output of:

not doing anything
#<WhatImDoing:0xb74b14e8>

I'm curious, what is the second line all about? Is it a reference location for the WhatImDoing object I created? Can I access objects through this location(like a pointer or something)? Etc... Just trying to get a better understanding of Ruby, in general.

Thanks.

2
  • I forgot to mention: I was curious if I could maybe access it via a pointer to that location? Commented Feb 22, 2013 at 19:33
  • Edit the question; don't add questions as comments. My answer explains what to do with the value; there are no "pointers" as such in Ruby, rather references to objects. Commented Feb 22, 2013 at 19:34

3 Answers 3

7

The second line is the output of irb, showing the return value of the last statement.

If you set something equal to that value:

> class WhatImDoing
    def initialize
      puts "not doing anything"
    end

    def ohai
      puts "Ohai"
    end
  end
> tmp = WhatImDoing.new
=> #<WhatImDoing:0x5cd5a2a9>

You could use it:

> tmp.ohai
Ohai

If you had a custom to_s it would show that instead:

> class WhatImDoing
    def to_s
      "#{super} kthxbai"
    end
  endt
> tmp = WhatImDoing.new
=> #<WhatImDoing:0x3e389405> kthxbai 
Sign up to request clarification or add additional context in comments.

Comments

4

I'm assuming that was the output of irb. Irb tried to print your object, i.e. convert it to a string. Since you didn't provide a custom to_s ("to string") method, your object inherited this one:

http://ruby-doc.org/core-1.9.3/Object.html#method-i-to_s

Returns a string representing obj. The default to_s prints the object’s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby programs returns “main.”

Further digging into the source code reveals that the hexadecimal number is, indeed, the memory address occupied by that object instance. There isn't really anything fancy you can do with that information, in Ruby. It's just a convenient way to generate an unique identifier for an object instance.

4 Comments

(Although the "meaning" of that number is implementation-dependent.)
Yes, taking in consideration virtual memory. It's a pointer value in C (%p in sprintf).
VM doesn't even enter in to it; a Ruby implementation may use whatever it wishes to identify "where" an object lives, or what it uses as the human-readable interpretation of that, etc. An object ID could simply be an incrementing index, for example, that points internally to a mapping table. My point is that "digging into the source" is only relevant for one specific implementation and could be very different across implementations--naming the implementation you're looking at is important.
Got it. I was thinking of the default reference implementation in C, but i should have been explicit about it.
2

Yes, it is reference to the object you are creating. Yes, you can access that object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.