# Meta programming problem

**URL:** <https://discuss.rubyonrails.org/t/meta-programming-problem/5450>\
**Category:** rubyonrails-talk\
**Created:** [December 4, 2006, 10:33pm UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450 "2006-12-04T22:33:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dj\_T](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dj\_T](https://discuss.rubyonrails.org/u/Dj_T)\
**Post date:** [December 4, 2006, 10:33pm UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450/1 "2006-12-04T22:33:50Z")

</div>

Hi,

i've wrote the following code module TeamScore &nbsp;&nbsp;class Filter &nbsp;&nbsp;&nbsp;&nbsp;ALLOWED\_RESOURCES = [:game]

&nbsp;&nbsp;&nbsp;&nbsp;# convert \<game:4 title\> into markdown link to game re &nbsp;&nbsp;&nbsp;&nbsp;# use game title as link name &nbsp;&nbsp;&nbsp;&nbsp;# "Google":[http://google.com](http://google.com).

---

<div class="post-metadata">

**Author:** ![Max\_Muermann](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Max\_Muermann](https://discuss.rubyonrails.org/u/Max_Muermann)\
**Post date:** [December 5, 2006, 1:25am UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450/2 "2006-12-05T01:25:14Z")

</div>

> Hi,
> 
> i've wrote the following code module TeamScore &nbsp;&nbsp;class Filter &nbsp;&nbsp;&nbsp;&nbsp;ALLOWED\_RESOURCES = [:game]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# convert \<game:4 title\> into markdown link to game re &nbsp;&nbsp;&nbsp;&nbsp;# use game title as link name &nbsp;&nbsp;&nbsp;&nbsp;# "Google":[http://google.com](http://google.com). &nbsp;&nbsp;&nbsp;&nbsp;# &nbsp;&nbsp;&nbsp;&nbsp;def self.filter(str) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.gsub(/\<([a-z]+):(\d+)\>/) do |match| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_link\_for($1.to\_sym,$2, match) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def self.create\_link\_for(resource\_type, id, match) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Macro #{match} not allowed" unless ALLOWED\_RESOURCES.include?(resource\_type.to\_sym) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case resource\_type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :game &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Game &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :article &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Article &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object = model.send("find", id)
> 
> "\"#{object.name}\":www.teamscore.org/#{object.class.name.pluralize.downcase}/#{object.id}" &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end
> 
> How i can dry case when block.

Remove it altogether 😉 see below.

> My idea is to send a find message to model coresponding the resource\_type argument but resource\_type is a string (previously extracted from regexp) and I can't use ressource\_type.send(....) or send("#{ressource\_type.classify}.find", id) the 2 stament raise an error (NoMethodError: undefined method `Game' for TeamScore::Filter:Class)

You don't need the send here. Send without an object tries to execute send on the current scope, which would be TeamScore::Filter. In this case, you are trying to execute self.Game.find(id), which is of course not valid (unless you happened to have a constant called Game defined on the class...).

Try resource\_type.classify.find(id).

resource\_type.classify will simply return the class (Game or Article), which you can happily call any methods on.

Cheers, Max

---

<div class="post-metadata">

**Author:** ![Dj\_T](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dj\_T](https://discuss.rubyonrails.org/u/Dj_T)\
**Post date:** [December 5, 2006, 8:03am UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450/3 "2006-12-05T08:03:52Z")

</div>

Max Muermann a écrit :

> Remove it altogether 😉 see below.
> 
> \> My idea is to send a find message to model coresponding the \> resource\_type argument but resource\_type is a string (previously \> extracted from regexp) and I can't use ressource\_type.send(....) \> or send("#{ressource\_type.classify}.find", id) the 2 stament raise an \> error (NoMethodError: undefined method `Game' for \> TeamScore::Filter:Class)
> 
> You don't need the send here. Send without an object tries to execute send on the current scope, which would be TeamScore::Filter. In this case, you are trying to execute self.Game.find(id), which is of course not valid (unless you happened to have a constant called Game defined on the class...).
> 
> Try resource\_type.classify.find(id).
> 
> resource\_type.classify will simply return the class (Game or Article), which you can happily call any methods on.
> 
> Cheers, Max

Thanks,

I've try it but this doesn't work. I've got an LocalJumpError or something like this. I think it try to invoke find on a String not on the Game or Article. The classify method just retunr a string in my mind.

---

<div class="post-metadata">

**Author:** ![Jean-Francois1](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jean-Francois1](https://discuss.rubyonrails.org/u/Jean-Francois1)\
**Post date:** [December 5, 2006, 9:56am UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450/4 "2006-12-05T09:56:23Z")

</div>

Guillaume :

> \> You don't need the send here. Send without an object tries to execute \> send on the current scope, which would be TeamScore::Filter. In this \> case, you are trying to execute self.Game.find(id), which is of course \> not valid (unless you happened to have a constant called Game defined \> on the class...). \> \> Try resource\_type.classify.find(id). \> \> resource\_type.classify will simply return the class (Game or Article), \> which you can happily call any methods on.
> 
> Thanks,
> 
> I've try it but this doesn't work. I've got an LocalJumpError or something like this. I think it try to invoke find on a String not on the Game or Article. The classify method just retunr a string in my mind.

Try :

&nbsp;&nbsp;resource\_type.classify.constantize.find(id)

&nbsp;&nbsp;&nbsp;&nbsp;-- Jean-François.

---

<div class="post-metadata">

**Author:** ![Dj\_T](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dj\_T](https://discuss.rubyonrails.org/u/Dj_T)\
**Post date:** [December 5, 2006, 7:02pm UTC](https://discuss.rubyonrails.org/t/meta-programming-problem/5450/5 "2006-12-05T19:02:25Z")

</div>

This work great.

Thank a lot.

Jean-François a écrit :
