I found the behavior of keyworddef in ruby is really confusing! At least, really confusing to me! In most case, we useclass context, then it defines a instance method on specific class. Besides the typical usage, we can also usedef in block. This previous piece of code works as we reopened the classFoo, and add a new method to it. It is also not hard to understand. The fact that really surprised me is in the following code: Here we can found that method foo goes into theFoo class itself, rather thanFoo‘s instance! But the $context is stillSo in a word, callingdef foo ininstance_eval block is equivalent to calling ‘def self.foo’ inclass_eval block, even though the context of both block are the class itself. So we can figure out that keyworddef works different than methoddefine_singleton_method, since it doesn’t depend on self, but the two methods does! To me it is kind of hard to understand. and confusing. And I think it is not a good design! Ruby is different to other Java or C#, ruby uses methods on class to take place of the keywords in other languages, such as public, protected and private. In most of the language, they are keywords. But in ruby they are actually the class methods of Class. This design is good, because it is kind of enabled the developer to extend the “keyword” they can use! But at the same time, this design melted the boundary between customizable methods and predefined keywords, so people won’t pay much attention to the difference of the two. So it is important to keep the consistency between methods and keyword behaviors. Butdef breaks the consistency, so it is confusing! Look the following code: A and classB, we can find that they are different, even they are defined with exactly same block!