The Artima Developer Community
Sponsored Link

Java Buzz Forum
Ruby Metaprogramming 201

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Ruby Metaprogramming 201 Posted: Apr 11, 2005 5:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Ruby Metaprogramming 201
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

If you find yourself doing a lot of eval(..) based metaprogramming, remember to include debug stuff!

begin
    eval("dsadh!!sadkhj how are you today?")
rescue Exception => e
    log.error "Metaprogramming error [#{e}]"
end

Which is nifty and all if the eval fails, but what happens on the way more subtle?

begin
  eval <<-EOM
    def foo
      robert is sick today
    end
  EOM
rescue Exception => e
  log.error "Metaprogramming error [#{e}]"
end

Where you don't get the syntax problem until you invoke foo. Solution!

eval <<-EOM
  def foo
    begin
      robert is sick today
    rescue Exception => e
      puts "Error while invoking foo defined from [insert stuff to help you debug]: \#{e}"
    end
  end
EOM 

You cannot catch syntax errors, but because so much of ruby uses ruby this isn't a big deal most of the time =) Note escaping the hash in \#{e} -- this is important because just #{e} would fail. Why is an exercise left to the reader ;-)

Read: Ruby Metaprogramming 201

Topic: Cenqua's gifts to software geeks Previous Topic   Next Topic Topic: Perl Deja Vu with new Groovy Syntax

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use