Hi,
I get each part of the following command in a string at runtime
Mod1::Util.parser1(“log.txt”)
So I have the following strings:
s1 = “Mod1”
s2 = “Util”
s3 = “parser1”
s4 = “log.txt”
Now I want to prepare the command and execute it at runtime.
Object::const_get(s1 + “::” + s2 + “.” + s3 + “(”" + s4 + “”)")
does not work.
Please help…
2010/8/20 Rajarshi C. [email protected]:
Now I want to prepare the command and execute it at runtime.
Object::const_get(s1 + “::” + s2 + “.” + s3 + “(”" + s4 + “”)")
does not work.
const_get will only resolve a constant (as the name indicates). You
could either use eval instead but I recommend the more robust and
secure version with const_get and send.
Object::const_get(s1).const_get(s2).send(s3, s4)
Cheers
robert
On Fri, Aug 20, 2010 at 1:12 PM, Rajarshi C.
[email protected] wrote:
Now I want to prepare the command and execute it at runtime.
Object::const_get(s1 + “::” + s2 + “.” + s3 + “(”" + s4 + “”)")
does not work.
If you know the first one is a module and the second a class, you could
try:
irb(main):001:0> module A
irb(main):002:1> class B
irb(main):003:2> def parse(param)
irb(main):004:3> puts “parsing #{param} …”
irb(main):005:3> end
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> s1 = “A”
=> “A”
irb(main):009:0> s2 = “B”
=> “B”
irb(main):010:0> s3 = “parse”
=> “parse”
irb(main):011:0> s4 = “filename.txt”
=> “filename.txt”
irb(main):014:0> m = Object.const_get(s1)
=> A
irb(main):015:0> c = m.const_get(s2)
=> A::B
irb(main):017:0> c.new.send(s3, s4)
parsing filename.txt …
Jesus.
Thank you, Robert and Jesus,
for the theory and the code.
