DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Natural Sentence In Ruby

11.12.2009
| 1621 views |
  • submit to reddit
        'Strange' class make this code syntacticly correct for ruby :

class Test < Strange
  def go
    isay hello the ruby is  {
      me "useful"
      me :beautiful
    }
    isay thee words off this sentence must be considered as free _method name by ruby engine {
      me "...?"
    }
  end
end

Test.new.go


execution give :

>I SAY hello the ruby is   useful
>I SAY hello the ruby is   beautiful
>I SAY thee words off this sentence must be considered as free method name by ruby engine   ...?

So here is the code of 'Strange'

class Strange
  def initialize
    @lmessage = []
  end
  def method_missing(id, *args)
    if block_given? 
      x=yield
      @lmessage,l=[],@lmessage
      return [:message,l,[id.to_s.gsub(/^_/,"")]]
    else
      args[0][2] << id.to_s.gsub(/^_/,"")
      return args[0]
    end
  end
  def isay(p) 
   p[1].each { |l| 
     puts ">I SAY " + p[2].reverse.join(" ") + l 
   } 
   @lmessage=[] 
  end
  def me(*t)  @lmessage << "   " + (t.inject([]) { |s,d|
     s << d.to_s 
    }.join(" "))  
  end
end

So now, is this useful ?