Over a million developers have joined DZone.

Natural Sentence In Ruby

·
'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 ?
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}