Forum: Ruby Unexpected syntax error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-02-20 01:13
(Received via mailing list)
$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

# Example to illustrate the problem
def foo1
   p (1..10).method(:each)
end

def foo2()
   p((1..10).method(:each))
end

def foo3()
   p (1..10).method(:each)
end

foo1 # -> No error
foo2 # -> No error
foo3 # -> Error

__END__

-- Daniel
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2006-02-20 01:24
Daniel Harple wrote:
> $ ruby -v
> ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]
>
> # Example to illustrate the problem
> def foo1
>    p (1..10).method(:each)
> end
>
> def foo2()
>    p((1..10).method(:each))
> end
>
> def foo3()
>    p (1..10).method(:each)
> end
>
> foo1 # -> No error
> foo2 # -> No error
> foo3 # -> Error
>
> __END__

This is quite bizarre. The parentheses on foo3 cause
the problem; leaving them out, adding a semicolon after
them or creating a temporary variable instead of the
direct p will all solve the error.

I would, though, venture that this should not be an
error :)

> -- Daniel


E
E51d56251ec4affafe85ee9367228965?d=identicon&s=25 Park Heesob (Guest)
on 2006-02-20 02:10
(Received via mailing list)
Hi,


>def foo1
>
>foo1 # -> No error
>foo2 # -> No error
>foo3 # -> Error
>
>__END__
>
>-- Daniel
>
It reminds me an old thread
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...


def foo1
  p (2.4/0.2).to_i
end
def foo2()
  p ((2.4/0.2).to_i)
end
def foo3()
  p (2.4/0.2).to_i
end

foo1 # -> 11
foo2 # -> 11
foo3 # -> 12.0

__END__

Regards,

Park Heesob
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-02-20 20:08
(Received via mailing list)
On Feb 19, 2006, at 4:10 PM, Daniel Harple wrote:

> end
>
> def foo3()
>   p (1..10).method(:each)
> end
>
> foo1 # -> No error
> foo2 # -> No error
> foo3 # -> Error

Ruby thinks those are argument parentheses.  This may not be a bug.

-:11: warning: don't put space before argument parentheses
#<Method: Range#each>
#<Method: Range#each>
1..10
-:11:in `method': undefined method `each' for class
`NilClass' (NameError)
         from -:11:in `foo3'
         from -:16
$ parse_tree_show -f
p (1..10).method(:each)
(eval):1: warning: (...) interpreted as grouped expression
[[:class,
   :Example,
   :Object,
   [:defn,
    :example,
    [:scope,
     [:block,
      [:args],
      [:fcall,
       :p,
       [:array, [:call, [:lit, 1..10], :method, [:array,
[:lit, :each]]]]]]]]]]

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-21 00:58
(Received via mailing list)
Hi,

In message "Re: Unexpected syntax error"
    on Tue, 21 Feb 2006 04:07:51 +0900, Eric Hodel
<drbrain@segment7.net> writes:

|Ruby thinks those are argument parentheses.  This may not be a bug.

This is a bug, and I fixed it last night(JST).

							matz.
This topic is locked and can not be replied to.