Confusion between precedence of unary minus and puts method

If I write

puts 10 - 5
# gives 5, same as
puts(10 -5)

Now if I write

puts(- 5)
#gives -5
But if I try -
puts - 5
# NoMethodError: undefined method `-' for nil:NilClass

It seems, puts - 5 is parsed as (puts) - 5.

But why not are we allowed to write the *unary* operation with `puts` like we are allowed with *binary* one ?

Regards,
Arup Rakshit

Use:

    puts -5

···

On 28 July 2014 17:34, Arup Rakshit <aruprakshit@rocketmail.com> wrote:

If I write

puts 10 - 5
# gives 5, same as
puts(10 -5)

Now if I write

puts(- 5)
#gives -5
But if I try -
puts - 5
# NoMethodError: undefined method `-' for nil:NilClass

It seems, puts - 5 is parsed as (puts) - 5.

But why not are we allowed to write the *unary* operation with `puts` like
we are allowed with *binary* one ?

Regards,
Arup Rakshit

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Because the parser needs a way to make a decision about ambiguous
syntax to remove ambiguity. The expression "puts - 5" parses the same
as "expr1 - expr2", i.e. a binary minus expression. There is really no
other way to interpret that without sacrificing consistency.

Kind regards

robert

···

On Mon, Jul 28, 2014 at 9:34 AM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:

If I write

puts 10 - 5
# gives 5, same as
puts(10 -5)

Now if I write

puts(- 5)
#gives -5
But if I try -
puts - 5
# NoMethodError: undefined method `-' for nil:NilClass

It seems, puts - 5 is parsed as (puts) - 5.

But why not are we allowed to write the *unary* operation with `puts` like
we are allowed with *binary* one ?

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/