# Module\_eval: create method that contains value passed to it?

**URL:** <https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666>\
**Category:** Ruby\
**Created:** [January 7, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666 "2006-01-07T20:48:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 7, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666/1 "2006-01-07T20:48:38Z")

</div>

This is a Rails example, but I think the problem is a general Ruby  
matter.

I am using class\_eval within a class method, foobar(), to have it create  
an instance method, get\_foobar(). I am passing a value (“testing testing  
123”) to foobar() that I want to make part of get\_foobar(). However, I  
can’t seem to include this value in the output of get\_foobar().

Where I want the “render” line to output “A start testing testing 123  
end O”, it only outputs “A start end O”. Extremely grateful for any  
help. Code:

```
class HelpController < ApplicationController

  def self.foobar(v)

    class_eval %q{
      def get_foobar()
        "start #{v} end"
      end
    }

  end

  foobar "testing testing 123"

   def index
    render :text => "A "+get_foobar+" O"
  end

end

```

Or syntax highlighted here: [http://rafb.net/paste/results/igothN18.html](http://rafb.net/paste/results/igothN18.html)

---

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 7, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666/2 "2006-01-07T22:34:41Z")

</div>

Henrik wrote:

> This is a Rails example, but I think the problem is a general Ruby  
> matter.

Perhaps, but it would be more helpful to isolate the problem by removing  
all Rails influence, since there is every chance that Rails is munging  
up core Ruby behavior.

But I think the problem is simply that you are not quoting your strings  
correctly:

class HelpController  
def self.foobar(v)  
class\_eval %Q{  
def get\_foobar()  
“start #{v} end”  
end  
}  
end

```
  foobar "testing testing 123"
   def index
    puts "A " + get_foobar + " O"
  end
end

```

HelpController.new.index # A start testing testing 123 end O

(Big Q, not little q)

I have no idea if this works on the railroad.

James

–

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 7, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666/3 "2006-01-07T23:44:06Z")

</div>

Henrik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end O", it only outputs “A start end O”. Extremely grateful for any  
> }
> 
> Or syntax highlighted here:  
> [http://rafb.net/paste/results/igothN18.html](http://rafb.net/paste/results/igothN18.html)

You don’t even need class\_eval - a simple closure is sufficient:

class X  
def self.foobar(x)  
define\_method(:get\_foobar) { x }  
end

foobar “testing testing 123”  
end

> > x=X.new  
> > =\> #\<X:0x101c1198\>  
> > x.get\_foobar  
> > =\> “testing testing 123”  
> > X.foobar “qwert”  
> > =\> #Proc:0x101c8530@:3(irb)  
> > x.get\_foobar  
> > =\> “qwert”

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 10, 2006, 1:23am UTC](https://www.ruby-forum.com/t/module-eval-create-method-that-contains-value-passed-to-it/94666/4 "2006-01-10T01:23:33Z")

</div>

On Sun, 8 Jan 2006, Henrik wrote:

> help. Code:
> 
> Or syntax highlighted here: [http://rafb.net/paste/results/igothN18.html](http://rafb.net/paste/results/igothN18.html)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

this can be done with closures or a storage/search-path scheme. the  
traits  
lib uses the latter, see here:

[http://codeforpeople.com/lib/ruby/traits/](http://codeforpeople.com/lib/ruby/traits/)

## -a

# =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | strong and healthy, | who thinks of sickness until it strikes like lightning? | preoccupied with the world, | who thinks of death, until it arrives like thunder? | – milarepa
