D
Demonic Software
[Note: parts of this message were removed to make it a legal post.]
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(c) => 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.
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(c) => 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.