41

I'm trying to create a class, which has a constructor that takes a single argument. When I create a new instance of the object, it returns a pointer.

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

What am I doing wrong? Thanks

binki
  • 7,754
  • 5
  • 64
  • 110
user1043856
  • 451
  • 1
  • 5
  • 7

4 Answers4

70

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
WarHog
  • 8,622
  • 2
  • 29
  • 23
  • 6
    It would be beneficial to use `attr_reader :my_num` here instead of defining your own method that does the same. – tadman Nov 14 '11 at 18:38
  • 1
    Of course, but I thought that using one getter method would be more clearer for topic starter :) Indeed, I've updated my message, thanks – WarHog Nov 14 '11 at 18:51
  • 5
    Note that attr_reader was mentioned and then attr_accessor was used. attr_reader does also work. It only allows you to get the value, while attr_accessor allows you to also assign a new value to my_num. – David Winiecki Sep 17 '14 at 02:19
15

You aren't doing anything wrong. Assuming you see something like #<Adder:0xb7f9f710 @my_num=12> then in Ruby this is just the default representation of the object that you've created.

If you want to change this behaviour to be more friendly when you pass your object to puts you can override the to_s (to string) method. e.g.

class Adder
  def initialize(my_num)
    @my_num = my_num
  end

  def to_s
    "Adder with my_num = #{@my_num}"
  end
end

then when you do puts y you'll see Adder with my_num = 12

You can also override the inspect method which is what is used, for example, when the Ruby irb console prints the representation of your object e.g.

class Adder
  def inspect
    to_s # return same representation as to_s
  end
end

then in irb:

>> y = Adder.new 12
=> Adder with my_num = 12
mikej
  • 65,295
  • 17
  • 152
  • 131
4

That's because the object is a pointer. In Ruby, all objects are allocated on the heap, and the variables are just references to them.

When you do

puts y

It is actually calling the default to_s method of the object, which is just to output the class name, the memory location, and some info on the instance variables of the object.

robbrit
  • 17,560
  • 4
  • 48
  • 68
4

Ruby does not have pointers. In your example, y is an instance of Adder with an instance variable called @my_num with the value of 12 (which is itself a Fixnum object).

The puts method calls the to_s method of whatever arguments you pass it. That's what you see output; perhaps you think that output refers to a pointer, but it's just a textual representation of the object. You can change it by overriding the to_s instance method for any class.

coreyward
  • 77,547
  • 20
  • 137
  • 166