# Setting Class Methods Using Strings

**URL:** <https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779>\
**Category:** Ruby\
**Created:** [March 13, 2008, 6:30am UTC](https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779 "2008-03-13T06:30:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsoftware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@dsoftware](https://www.ruby-forum.com/u/dsoftware)\
**Post date:** [March 13, 2008, 6:30am UTC](https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779/1 "2008-03-13T06:30:11Z")

</div>

Hello,

First question:  
With the 'print ‘foo\_method called with #{arg}’, is there a way to  
escape  
the “#” so that arg will not be evaluated until after the statement is  
evalled? For example:

Let c be “'print ‘foo\_method called with #{arg}’”

eval© =\> print ‘foo\_method called with #{arg}’

Second (Main Question)  
Is it possible to set an object’s methods with raw code and a method  
name?  
I am not sure how to Google for an example on this, so I will just show  
an  
example.

== Here is an empty class definition Foo, and later on I will want to  
assign/add/set a method in an instantiated Foo object ==

class Foo  
end

== Now I want to set a method in an instantiated Foo with code, so that  
I  
can call f ==

code = “def foo\_method(arg)  
print ‘foo\_method called with #{arg}’  
end”

f = Foo.new()  
#…  
#code to set a method in f  
#…

f.foo\_method(“for the win!”)

==

Is this possible? or Do I need create a string and add the method in  
like  
the following example?

code = “\ndef foo\_method(arg)  
print ‘foo\_method called with #{arg}’  
end\n”  
foo = “class Foo " + code + " end”

eval(foo)  
f = Foo.new()  
f.foo\_method(“for the win!”)

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![dsoftware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@dsoftware](https://www.ruby-forum.com/u/dsoftware)\
**Post date:** [March 13, 2008, 6:50am UTC](https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779/2 "2008-03-13T06:50:42Z")

</div>

On Mar 12, 11:29 pm, Demonic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class Foo  
> #…  
> code = “\ndef foo\_method(arg)  
> print ‘foo\_method called with #{arg}’  
> end\n”  
> foo = “class Foo " + code + " end”
> 
> eval(foo)  
> f = Foo.new()  
> f.foo\_method(“for the win!”)
> 
> Thanks in advance for your help.

If you know when to use single quotes and when to use double quotes,  
yes to both:

a = 1  
c = ‘puts “a = #{a}”’ # =\> “puts "a = #{a}"”  
eval(c) # \>\> a = 1

code = ‘def foo\_method(arg); puts “foo\_method called with #{arg}”;  
end’  
class Foo; end  
Foo.class\_eval(code)  
Foo.new.foo\_method(‘hi’) # \>\> foo\_method called with hi

Single-quoted strings do not perform interpolation, while double-  
quoted strings do. So if you juggle them for eval, you can have it do  
what you want. Of course, putting a backslash before the #{} in double-  
quoted strings will escape that so it doesn’t perform the  
interpolation.

Play around and you should get the hang of it quickly. And beware  
eval. Use carefully.

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![dsoftware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@dsoftware](https://www.ruby-forum.com/u/dsoftware)\
**Post date:** [March 13, 2008, 7:08am UTC](https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779/3 "2008-03-13T07:08:17Z")

</div>

Cool thanks Chris.

---

<div class="post-metadata">

**Author:** ![dsoftware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@dsoftware](https://www.ruby-forum.com/u/dsoftware)\
**Post date:** [March 13, 2008, 8:15am UTC](https://www.ruby-forum.com/t/setting-class-methods-using-strings/131779/4 "2008-03-13T08:15:16Z")

</div>

On Thu, Mar 13, 2008 at 12:29 AM, Demonic S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Foo  
> #…  
> code = "\ndef foo\_method(arg)  
> Thanks in advance for your help.  
> Unless you absolutely _have_ to create a string (like you’re holding  
> code in a database that is to be executed)…

class Foo  
end

f = Foo.new  
g = Foo.new

def f.bar arg; puts arg; end

f.bar “hi”

# “hi”

g.bar “hi”

# undefined method error

It can also be done like this after the class construction…

class \<\< f  
def bar arg; puts arg; end  
end

I don’t think this case is a good use of #eval, but it depends on what  
you’re doing.

hth,  
Todd
