# Finding methods inside a scope

**URL:** <https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289>\
**Category:** Ruby\
**Created:** [August 22, 2012, 1:20pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289 "2012-08-22T13:20:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 22, 2012, 1:20pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/1 "2012-08-22T13:20:06Z")

</div>

Suppose I have a scope

plate do  
serve do |d|  
d.pizza 2  
d.momo 5  
end

```
eat do |d|
  d.pizza 1
  d.momo 2
end

```

end

Is there a way to find that exactly two methods, namely serve and eat  
have been defined under plate’s scope.

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 22, 2012, 1:45pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/2 "2012-08-22T13:45:39Z")

</div>

Hi,

Those are method _calls_, not method definitions.

I’m pretty sure listing method calls is complicated and requires a deep  
analysis of Ruby internals. What do you need that for?

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 22, 2012, 6:04pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/3 "2012-08-22T18:04:12Z")

</div>

I’m instance\_eval-ing the plate block and then trying to hook up method  
missing to call serve and eat methods. The problem is that this is on a  
class level scope so every-undefined-method passes on to method missing.  
I could get away with defining a pattern and then using a regex but  
since it is enclosed in the plate block, I was wondering if there was a  
more natural way available to infer methods/method calls that happen  
inside the block scope. Does this make sense?

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/4 "2012-08-27T14:01:30Z")

</div>

On Wed, Aug 22, 2012 at 6:04 PM, Dipesh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m instance\_eval-ing the plate block and then trying to hook up method  
> missing to call serve and eat methods. The problem is that this is on a  
> class level scope so every-undefined-method passes on to method missing.

Yes, that’s regular behavior for every object.

> I could get away with defining a pattern and then using a regex but  
> since it is enclosed in the plate block, I was wondering if there was a  
> more natural way available to infer methods/method calls that happen  
> inside the block scope. Does this make sense?

I still do not understand what your issue is: if you know where you  
want to invoke those methods then method\_missing can simply delegate  
to the instance. Other than that, what else do you want to do with  
methods which are not defined and of which you do not know where they  
are?

Can you show more code, namely the code which invokes instance\_eval  
and demonstrate what it is supposed to do?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/5 "2012-08-27T14:01:35Z")

</div>

On Wed, Aug 22, 2012 at 1:45 PM, Jan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Those are method _calls_, not method definitions.

Also the call to #plate does not introduce a scope. If at all the  
scope is opened by the block passed to #plate.

> I’m pretty sure listing method calls is complicated and requires a deep  
> analysis of Ruby internals.

For a first start one can do

plate do  
p method(:serve), method(:eat)

```
serve do |d|
  d.pizza 2
  d.momo 5
end

eat do |d|
  d.pizza 1
  d.momo 2
end

```

end

The output will tell you something about the class in which the method  
is defined.

> What do you need that for?

I second that question.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 4:32pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/6 "2012-08-27T16:32:01Z")

</div>

On Mon, Aug 27, 2012 at 4:12 PM, Dipesh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Sorry, I should have posted more code. The code below does not work but  
> I  
> think you will get the idea.
> 
> class Food  
> def plate(&block)  
> extend Operators  
> include Operations

That line above does not make sense since you can only include a  
module in a class or module. Also, it’s a questionable habit to  
extend multiple times with the same module. That is inefficient.

> instance\_eval(&block)  
> end
> 
> plate do  
> eat do |d|  
> d.pepsi, 20  
> d.coke, 10  
> end  
> end

Method plate is not defined for the class. Also it’s a bad idea to  
have similarly named methods for class and instance.

> end  
> I wanted it to trigger the pepsi and coke methods on instance level with  
> corresponding  
> arguments. I have not included those methods here though.

Why? How are we supposed to understand your use case?

> I wanted to define my methods inside the Operations module via method  
> missing. But since it was extended, I was looking for a scope to tie up  
> to.

> Hope that made sense.

Frankly, no - at least not to me.

> And Thanks. If you guys say there’s a better way  
> available, I would be happy to post the working code.

I would need to understand what your goal is. It’s not clear to me  
yet. Can you describe your use case more abstractly than with a  
solution in mind? What are your requirements and goals?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 4:12pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/7 "2012-08-27T16:12:35Z")

</div>

Sorry, I should have posted more code. The code below does not work but  
I  
think you will get the idea.

class Food  
def plate(&block)  
extend Operators  
include Operations  
instance\_eval(&block)  
end

plate do  
eat do |d|  
d.pepsi, 20  
d.coke, 10  
end  
end

end

module Operations  
attr\_accessor :bloc  
def serve(&block)  
@bloc[:serve] = block  
end  
def eat(&block)  
@bloc[:eat] = block  
end  
end

module Operators  
def serve!  
@bloc[:serve].call(self)  
end  
end

Then, when I do  
Food.new.serve!

I wanted it to trigger the pepsi and coke methods on instance level with  
corresponding  
arguments. I have not included those methods here though.

I wanted to define my methods inside the Operations module via method  
missing. But since it was extended, I was looking for a scope to tie up  
to.

But, I got it wrong there. Later, I got around it by doing something  
like:

class Food

def plate(&block)  
@current\_scope = **method**  
instance\_eval(&block)  
@current\_scope = nil  
end

end

module Operations  
def method\_missing(m,\*a,&b)  
if @current\_scope == :plate  
@bloc[:m] = b  
else  
super  
end  
end  
end

Hope that made sense. And Thanks. If you guys say there’s a better way  
available, I would be happy to post the working code.

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 6:34pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/8 "2012-08-27T18:34:47Z")

</div>

Hi,

To put it bluntly: I think you’ve kind of got lost in all that  
metaprogramming magic and forgot what you actually want to do. We’ve  
asked you again and again about your goal, and you could never give a  
concrete answer (and your code doesn’t tell either).

Your code is so packed with obscure techniques (like all that module  
extending) and strange dependencies  
that I find it almost impossible to read. When I see something like  
this, I immediately think: This guy (or gal) doesn’t think clearly.

Even complex algorithms usually don’t use metaprogramming that heavily.  
So I think there’s something wrong with how you program. You’ve almost  
developed your own “pseudo-OOP” where you use modules and extending  
instead of classes and inheritance.

I know this doesn’t sound nice, but what I would do is completely dump  
the code and start from scratch. First you should ask yourself: What do  
I want to achieve? Why do I even need the code? Then you should create  
an appropriate and _simple_ object model. Use metaprogramming only when  
it’s actually necessary. The normal way of doing things is using classes  
and fixed methods.

Of course we can also help you with the concept. But then we’ll need a  
concrete goal. Something like “I want to write a database wrapper” or “I  
want to write a text-based adventure game”.

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 5:02pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/9 "2012-08-27T17:02:30Z")

</div>

> <https://gist.github.com/dgtm/3489255>

What I was trying to do was to define methods inside Events via  
method\_missing.

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 7:37pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/10 "2012-08-27T19:37:19Z")

</div>

Hmm…  
At certain events of my program, I send an email(whose template differs  
on what event just happened), update some redis statistics, create and  
update some database entries and do some other calculations. I do this  
set of actions repetitively for 12-15 events, for some events, I might  
not need to send an email or create a db entry and such. I wanted to  
create a nice  
DSL and then keep the actual methods separate from the class which was  
turning into a hell of method calls. I was thinking the DSL would serve  
as a easy place to change things on a higher level, that was what I had  
in mind.

And no, it actually sounds nice to dump the code but I fear I will  
somehow be going along the same path again.

---

<div class="post-metadata">

**Author:** ![dipeshgtm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@dipeshgtm](https://www.ruby-forum.com/u/dipeshgtm)\
**Post date:** [August 27, 2012, 11:08pm UTC](https://www.ruby-forum.com/t/finding-methods-inside-a-scope/222289/11 "2012-08-27T23:08:58Z")

</div>

On Mon, Aug 27, 2012 at 7:37 PM, Dipesh G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hmm…  
> At certain events of my program, I send an email(whose template differs  
> on what event just happened), update some redis statistics, create and  
> update some database entries and do some other calculations. I do this  
> set of actions repetitively for 12-15 events.

Do you mean 12-15 different types of events?

> I wanted to create a nice  
> DSL and then keep the actual methods separate from the class which was  
> turning into a hell of method calls. I was thinking the DSL would serve  
> as a point of contact, that was what I had in mind.

Are users supposed to change the handling of events?

> And no, it actually sounds nice to dump the code but I fear I will  
> somehow be going along the same path again.

🙂 Maybe we can counsel you to some clarifications of what you are  
trying to do. I found that lacking clarity is one source for badly  
structured code. And talking to others - especially from the same  
field - helps in finding clarity in one’s own mind. I can only speak  
for me of course but I have observed the phenomenon with a few  
colleagues as well so I reckon it must be generally helpful to  
communicate about ideas and code.

Kind regards

robert
