button , I don’t have to put an object or a class name in front of it .
def some_method
end
puts w.value # this should output 20
Is this possible , without adding those methods to Object ?
Hi, if you use the following you can call certain methods like you want.
class Whatever
attr_accessor :value
def initialize &blk
instance_eval &blk
end
def some_method
puts “some method”
end
end
This will allow to do:
w = Whatever.new {some_method}
The only exception are the xxx= methods. This won’t work:
w = Whatever.new {value = 20}
The reason is that value= methods have to be called as self.value=20
within the class, because it’s
the only way for the interpreter to understand that you want a method
call and not a local variable to the
function. You could do:
w = Whatever.new {self.value = 20}
That works.
Hope this gives you some other ideas…
Jesus.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.