From: Nokan Emiro Date: 2013-01-25T02:30:52+09:00 Subject: building expressions in strings --e89a8ff252ee0ff17104d40c1458 Content-Type: text/plain; charset=UTF-8 Hi list, In my program I need to combine together two Ruby expressions with a binary operator or apply an unary one on an expression. The expressions are given in strings and the returned expression is a String too. def binary(exp1, op, exp2) if %i[ + - * ** & | && || ].member? op "(#{exp1}) #{op} (#{exp2})" # infix format else "(#{exp1}).#{op}(#{exp2})" # method call format end end I would like to know if there's a simple way to eliminate the safety parentheses if they are not necessary. binary("1 + 1", :*, "2 + 2") needs the parentheses, but binary("1 + 1", :+, "2 + 2") doesn't. Can Ripper or any additional gem elevate the main operator (I mean the operator that would be evaluated lastly) in an expression to make it possible to check precedence? How do I know if I really need the parentheses before the . in the method call format line? --e89a8ff252ee0ff17104d40c1458 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi list,

In my program I need to combine together two Ru= by expressions with
a binary operator or apply an unary one on an= expression. =C2=A0The expressions
are given in strings and the r= eturned expression is a String too.

def binary(exp1, op, exp2)
=C2=A0 if %i[ + - * ** & | && || ].member? op
=
=C2=A0 =C2=A0 "(#{exp1}) #{op} (#{exp2})" # infix format
=C2=A0 else
=C2=A0 =C2=A0=C2=A0"(#{exp1}).#{op}(= #{exp2})" # method call format
=C2=A0 end
end

I would like to know if there's a simple way to eliminate the sa= fety parentheses
if they are not necessary. =C2=A0binary("1 + 1", :*, "2= + 2") needs the parentheses, but
binary("1 + 1", = :+, "2 + 2") doesn't.

Can Ripper or = any additional gem elevate the=C2=A0main operator (I mean the operator that= would
be evaluated lastly) in an expression to=C2=A0make it possible to chec= k precedence?

How do I know if I really need the p= arentheses before the . in the method call format line?


--e89a8ff252ee0ff17104d40c1458--