0

I want to add new method to Integer class, but I have no idea how to access integer value in this method:

class Integer
  def foo
    'foo' * value
  end
end

It should work like:

3.foo
=> 'foofoofoo'
zerozero7
  • 383
  • 3
  • 4
  • 10

1 Answers1

5

Using self:

class Integer
  def foo
    'foo' * self
  end
end
#It should work like:

p 3.foo
#=> 'foofoofoo'

You can also use Kernel#__method__ for a more general approach:

class Integer
  def foo
    __method__.to_s * self
  end
end

p 3.foo
#=> 'foofoofoo'
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35