Hi
I am fine tuning a DSL and came across the situation
where I needed a method with the name of 'module'.
In my previous revision of the DSL, I did the following:
···
------------
class C
def initialize
yield self
end
def module
puts "in C#module"
end
end
-------------
The user code would then look like:
C.new do |c|
c.module
end
But, I didn't really like requiring (or explaining)
why the 'c.' was required. So I rewrote the class to be:
------------
class C
def initialize(&block)
instance_eval &block
end
def module
puts "in C#module"
end
end
-------------
But, when a method name is called that is dear to Ruby heart,
like 'module', an error is generated:
C.new do
module
end #=> syntax error
Is there any way to have get the effect of the above
c.module
with the instance_eval?
Thanks
--
Jim Freeze
Robert
(Robert)
2
Jim Freeze wrote:
Hi
I am fine tuning a DSL and came across the situation
where I needed a method with the name of 'module'.
In my previous revision of the DSL, I did the following:
------------
class C
def initialize
yield self
end
def module
puts "in C#module"
end
end
-------------
The user code would then look like:
C.new do |c|
c.module
end
But, I didn't really like requiring (or explaining)
why the 'c.' was required. So I rewrote the class to be:
------------
class C
def initialize(&block)
instance_eval &block
end
def module
puts "in C#module"
end
end
-------------
But, when a method name is called that is dear to Ruby heart,
like 'module', an error is generated:
C.new do
module
end #=> syntax error
Is there any way to have get the effect of the above
c.module
with the instance_eval?
No. Unless you resort to using string instead of a block and do some
gsub'bing on the string which inserts the "c." part (or "self." in that
case).
"module" and "class" are keywords. That's why you never can do
"class.new..." but always have to "self.class.new...".
Kind regards
robert