Practical1
Practical1

Reputation: 678

Instance method only outputs class id, not method output

I was trying to see if I could transfer an instance variable's attributes using a simple do statement.

class Dog
  def initialize(name, age)
    @name = name
    @age = age
  end

  def bark
    puts 'Woof'
    puts 'Woof'
    puts 'Woof'
  end
end

bo = Dog.new do |word|
  word.bark
end

Why does it return the class id instead of the function's output?

Upvotes: 0

Views: 72

Answers (1)

Ilya Konyukhov
Ilya Konyukhov

Reputation: 2791

As it is suggested in comments, passing block to new statement makes no sense.

You can change your code to:

bo = Dog.new('Rusty', 3).tap do |word|
  word.bark
end

and it will bark as expected. :)

Upvotes: 5

Related Questions