# Something inconsistent in ruby syntax?

**URL:** <https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100>\
**Category:** Ruby\
**Created:** [June 12, 2006, 5:10am UTC](https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100 "2006-06-12T05:10:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 12, 2006, 5:10am UTC](https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100/1 "2006-06-12T05:10:45Z")

</div>

Please take a look at some exemplary code first.

class MailTruck  
instance\_eval \<\<-EOS  
define\_method(:hello1) {puts “hello1”}  
EOS

```
define_method(:hello2) {puts "hello2"}

instance_eval <<-EOS
	def hello3
		puts "hello3"
	end
EOS

def hello4
		puts "hello4"
end

```

end

hello1 and hello2 are instance methods.  
hello3 is a class method.  
hello4 is a instance method.

Why hello1 and hello2 are same, but hello3 and hello4 are different?

It seems a little inconsistent . Somebody can give a clear explanation?  
Thanks in advance.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 12, 2006, 6:21am UTC](https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100/2 "2006-06-12T06:21:37Z")

</div>

On Jun 11, 2006, at 11:10 PM, uncutstone wu wrote:

> uncutstone  
> hello1, hello2, and hello4 are all equivalent. They are the same  
> thing as writing

class A  
def hello1  
…  
end

def hello2  
…  
end

def hello4  
…  
end  
end

For hello3 you’ve done the equivalent of

class A  
def self.hello3  
…  
end  
end

The reason doesn’t happen with hello1 is

instance\_eval { # self is MailTruck in this block  
define\_method(…) { … } # this is like self.define\_method(…)  
{ … }  
# since define\_method is a method,  
# which is the same as define\_method  
outside of the instance\_eval in this case

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 12, 2006, 6:47am UTC](https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100/3 "2006-06-12T06:47:20Z")

</div>

Logan C. wrote:

> The reason doesn’t happen with hello1 is
> 
> instance\_eval { # self is MailTruck in this block  
> define\_method(…) { … } # this is like self.define\_method(…)  
> { … }  
> # since define\_method is a method,  
> # which is the same as define\_method  
> outside of the instance\_eval in this case

So I can say the reason is that define\_method is a method and def isn’t  
a method but a reserved keyword.

Thanks for your explanation, I think I’ve got it now.

uncutstone

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [June 12, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/something-inconsistent-in-ruby-syntax/63100/4 "2006-06-12T22:36:05Z")

</div>

On 6/12/06, uncutstone wu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> instance_eval <<-EOS
> 
> ```
> 
> hello1 and hello2 are instance methods.  
> hello3 is a class method.

I am as surprised as you are and I could not find any hint in the  
documentation why it should be?  
instance\_eval is an equivalent to self.instance\_eval, I fail to see  
any  
reason why the method definition is  
evaluated as a class method.

Can anyone explain while instance\_eval without a receiver could change  
the  
context of self?

Ty in advance

Robert

hello4 is a instance method.

> Why hello1 and hello2 are same, but hello3 and hello4 are different?
> 
> It seems a little inconsistent . Somebody can give a clear explanation?  
> Thanks in advance.

I agree

Robert

> uncutstone
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
