1

I have a strange situation where the code:

c = Class.new { eval parser }

... works in IRB (ruby 1.9.3) but not in code. I've tried it with and without 'class Foo' wrapping my methods. This is frustrating, to say the least. I can copy the string parser's content directly into variable parser in irb, and then create the class using the above line and my methods are all there but when I do it in code, they aren't.

7
  • What is your error in you code ? Commented Feb 15, 2012 at 15:27
  • There is no error until I try to instantiate the new class. When I try to use my updated initialize (which passes it a DB handle) that fails. Commented Feb 15, 2012 at 16:50
  • Maybe a more complete snippet of code can help Commented Feb 15, 2012 at 16:56
  • " def initialize( db ) @data = Hash.new @db = db @source = \"feeds.feedburner.com/FooFeed\" end def already? end def nextcid end def save_ctables end def fetch end def savedata end " . /home/devel/recc_ruby_mysql/ruby/Content/FetcherBuilder.rb:154:in initialize': wrong number of arguments(1 for 0) (ArgumentError) from /home/devel/recc_ruby_mysql/ruby/Content/FetcherBuilder.rb:154:in new' Commented Feb 15, 2012 at 17:36
  • The string in parser is (snipped) shown in the previous message although it doesn't come clean in the SO editor. code fetchclass = Class.new { eval parser } inst = fetchclass.new( @db ) puts inst.instance_variables.to_s code Commented Feb 15, 2012 at 17:39

2 Answers 2

2
C = Class.new
C.class_eval(code)

...Works for me in Ruby 1.9.2, even when saved in a file. Can you try and see if it works in 1.9.3?

Sign up to request clarification or add additional context in comments.

5 Comments

Same error. code fetchclass = Class.new fetchclass.class_eval( parser ) puts fetchclass.instance_methods.to_s ## doesn't show my methods inst = fetchclass.new( @db ) # crashes code Or, should I include the entire class def in the string including class Foo?
I am dragging down 1.9.2p290 to install over 1.9.3, since the ruby-debuug19 also appears to choke on 1.9.3. :)
1.9.2p290 does not die with the initialize( @db ) but it still doesn't contain the methods and fails when I try to call fetch. ruby-debug19 doesn't work here either, so I can't dig in.
I'm going to try using the exact code as you posted tomorrow. I'll edit my answer if I find a solution.
I've learned that it isn't a matter of the initialize() method. It fails when I use standard initialize() and then a separate initialize_db( @db ) call.
0

I've solved it.

The situation was that I was dynamically creating the definitions in order to make an optimized parser, and I was building it using incrementally added strings. As you can imagine, there was a lot of quote-escaping involved, especially with the MySQL queries. When I tested in irb, I forgot that using

puts parser

... evaluated the string while printing, removing one level of escaping while doing so.

The solution was simple: eval my string before class_eval'ing it.

fetchclass = Object.const_set(
                characteristics['shortname'],
                Class.new { class_eval( eval parser ) } )

1 Comment

Glad to hear that you nailed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.