This version of Vestibule is read only. It represents an archive of the community effort to produce content for Ruby Manor 4.

Parsing with evaluate for fun and profit

updated over 7 years ago; latest suggestion over 7 years ago

We all know that instance_eval() can execute some ruby in a context. How about using it to parse simple statements of your chosen DSL?

Text as a form of dynamic input isn't a much discussed thing, even though developers take it for granted that a unix shell is very useful.

As I am developing a game in ruby that responds to simple verb / noun sentences to manage a space ship, I can discuss

  • the basics of evaluation order,
  • how method_missing helps you out
  • implementing completion
  • some security basics
  • a little bit of grammar and grammar tree
  • taking a modular approach

I want to emphasize the basic techniques you can use, and the things to consider.

This is a simple example of letting ruby's parse do some work for you:

class Nasa

  def launch arg=nil
    puts "Going to #{arg}!"

  def rocket arg=nil

  def the arg=nil

  def to arg=nil

  def moon arg=nil
    "the Moon"

  def mars arg=nil
    "the Red Planet"

  def self.evaluate script

  def method_missing (methId, *args)
    puts "We can't go there."

Nasa.evaluate("launch rocket to mars") # => Going to the Red Planet!
Nasa.evaluate("launch rocket to moon") # => Going to the Moon!
Nasa.evaluate("launch rocket to mekon") # => We can't go there.


  • 35773545c3bb885b42cc24ccc0d1c666 ntlk suggests over 7 years ago

    What could be other practical applications of this? I'd like to know what else I could use it for. Are there some really interesting use cases or real world examples you could provide?

  • The proposal author responds over 7 years ago

    Mr Steele - oops, yes unclear. There are a number of similar statements, I use instance_eval() but I will correct this in my first sentence

    Mr Stuart - As the writers of South Park always say "The Simpsons have already done it" - thanks for the link.

    Mr Adam - More a case of how parsing supports a DSL; I'll think of some example to illuminate where I'm going.

  • Be3698f145a80c1230fd667c87d0f0c8 Tom Stuart suggests over 7 years ago

    I don't have any particular suggestion other than to say that this proposal reminds me of Martin Fowler's distinction between internal and external DSLs, which could be useful here.

  • B68ce3695bb8dc29b9f9cb0dc0b721a5 Murray Steele suggests over 7 years ago

    I'm probably being a bit dim, but by evaluate do mean using eval()? Or is there some other technique / tool you mean?

  • Acd62030df551952268e84c8fff26a5b James Adam suggests over 7 years ago

    I think it would strengthen this proposal to give a hint at the kinds of DSLs that you've been using. It sounds like you have a concrete example (the space ship game) - perhaps you can provide a teaser in your proposal to show what people will be able to do if they employ the techniques you're going to cover?