highlighter for operator precedence

M

Mike Schwab

Is there a web site that will highlight code to show who Ruby will send
each message to? I like playing around with parentheses and the ternary
operator, and I want to explore the precedence distinction between {}
and do end.

Some find it unnecessary to remove all implied parentheses and still
strive for one-liners, but I find it makes the code more readible, and
more emulatable. I think a visual tool for learning these rules could
make Ruby more accessible.

Once that were ready, would it not be a small step to make a tool that
strips the unnecessary parens itself?
 
R

Robert Klemme

Is there a web site that will highlight code to show who Ruby will send
each message to? I like playing around with parentheses and the ternary
operator, and I want to explore the precedence distinction between {}
and do end.

I do not know such a site. But you can do something like this to see
what happens:

irb(main):011:0> baz=1
=> 1
irb(main):012:0> def foo(*a) [:foo, a] end
=> nil
irb(main):013:0> def bar(*a) [:bar, a] end
=> nil
irb(main):014:0> foo bar baz
(irb):14: warning: parenthesize argument(s) for future version
=> [:foo, [[:bar, [1]]]]
irb(main):015:0> foo bar(baz)
=> [:foo, [[:bar, [1]]]]
irb(main):016:0> foo(bar baz)
(irb):16: warning: parenthesize argument(s) for future version
=> [:foo, [[:bar, [1]]]]
irb(main):017:0>
Some find it unnecessary to remove all implied parentheses and still
strive for one-liners, but I find it makes the code more readible, and
more emulatable. I think a visual tool for learning these rules could
make Ruby more accessible.

Well, if it suits you well why don't you just leave the parens in? From
time to time I tend to use parens in boolean expressions even though I
know that && binds before || just to not have to worry about precedence.
Once that were ready, would it not be a small step to make a tool that
strips the unnecessary parens itself?

Apparently nobody has bothered so far to do it so it must be a minor
problem. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top