From: 7stud -- Date: 2009-08-25T17:32:30+09:00 Subject: Re: operators vs. methods Sarah Allen wrote: > 7stud -- wrote: >> Ruby gives you a choice with some methods, like +() and =(), to use a >> special syntax. Special syntaxes are commonly known as "syntactic >> sugar". What happens is that the "sugared syntax" is converted into >> the normal method call, so: >> >> 1 + 2 becomes 1.+(2) >> >> and >> >> obj.x = 10 becomes obj.x=(10) (where the method name is 'x=') > > So, where might I find a list of the special sugary methods? I don't know. > and may I > define my own? Not that I'm aware of. However, you can define your own parser to replace the sugared syntax with a method call: r3test.rb ------ class Apple def shout(str) puts "#{str.upcase}!!!" end end a = Apple.new a shout "hello" #<---'SUGARED' METHOD CALL ----- ---- class MyRubyParser attr_reader :data, :results def initialize(fname) @data = File.open(fname).readlines @results = [] end def desugar(meth_name) @data.each do |line| line = line.chomp result = line.gsub(/^(.+?) (#{meth_name}) (.+)$/, '\1.\2(\3)' ) results << result end end end parser = MyRubyParser.new("r3test.rb") parser.desugar("shout") program = parser.results.join("\n") eval(program) --output:-- HELLO!!! -- Posted via http://www.ruby-forum.com/.