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'
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'
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'