Metaprogramming Ruby chapter 3 has a task to write a Ruby equivalent of C#'s using
statement. I started:
class Resource
def dispose
@disposed = true
end
def disposed?
@disposed
end
end
def using(r)
puts "Not implemented."
end
r = Resource.new
using(r)
I have not implemented using
yet. Nevertheless, when I run this code, I get
in `using': wrong argument type Resource (expected Module) (TypeError)
Furthermore, if I write something like using(Kernel)
, using(Enumerable)
, etc., the program finishes without error. As far as I know, there is no using
method or keyword in Ruby, but I also get the same behaviour in pry and irb. What is happening?
If you want to do that in Ruby 2.1 you will need to patch the main
object, as it already has the method like it's mentioned in the comments:
self.instance_eval do
def using(r)
puts "Not implemented."
end
end