# Keywords, &blocks and instance\_eval

**URL:** <https://rubytalk.org/t/keywords-blocks-and-instance-eval/19395>\
**Category:** ruby-talk\
**Created:** [7 July 2005 15:02 UTC](https://rubytalk.org/t/keywords-blocks-and-instance-eval/19395 "2005-07-07T15:02:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Freeze2](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jim\_Freeze2](https://rubytalk.org/u/Jim_Freeze2)\
**Post date:** [7 July 2005 15:02 UTC](https://rubytalk.org/t/keywords-blocks-and-instance-eval/19395/1 "2005-07-07T15:02:33Z")

</div>

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:

> **···**
>
> ------------  
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in C#module"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> -------------
> 
> The user code would then look like:
> 
> &nbsp;&nbsp;C.new do |c|  
> &nbsp;&nbsp;&nbsp;&nbsp;c.module  
> &nbsp;&nbsp;end
> 
> But, I didn't really like requiring (or explaining)  
> why the 'c.' was required. So I rewrote the class to be:
> 
> ------------  
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &block  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in C#module"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> -------------
> 
> But, when a method name is called that is dear to Ruby heart,  
> like 'module', an error is generated:
> 
> &nbsp;&nbsp;C.new do  
> &nbsp;&nbsp;&nbsp;&nbsp;module  
> &nbsp;&nbsp;end #=\> syntax error
> 
> Is there any way to have get the effect of the above
> 
> c.module
> 
> with the instance\_eval?
> 
> Thanks  
> --  
> Jim Freeze

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [7 July 2005 15:10 UTC](https://rubytalk.org/t/keywords-blocks-and-instance-eval/19395/2 "2005-07-07T15:10:46Z")

</div>

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:
> 
> ------------  
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in C#module"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> -------------
> 
> The user code would then look like:
> 
> &nbsp;&nbsp;C.new do |c|  
> &nbsp;&nbsp;&nbsp;&nbsp;c.module  
> &nbsp;&nbsp;end
> 
> But, I didn't really like requiring (or explaining)  
> why the 'c.' was required. So I rewrote the class to be:
> 
> ------------  
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &block  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in C#module"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> -------------
> 
> But, when a method name is called that is dear to Ruby heart,  
> like 'module', an error is generated:
> 
> &nbsp;&nbsp;C.new do  
> &nbsp;&nbsp;&nbsp;&nbsp;module  
> &nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;robert
