* splat error

Discussion in 'Ruby' started by Kerwin Franks, May 4, 2010.

  1. Hello, i need some help here, when i run the following code i get the
    error below. Why is that, according to the textbook it should work, also
    looked it up in the cookbook, no luck there too. Thanks in advance.

    def split_apart(first, *splat, last)
    puts "first: #{first.inspect}, splat: #{splat.inspect}, " +
    "last: #{last.inspect}"
    end

    split_apart(1,2)
    split_apart(1, 2, 3)

    Error:

    /home/gribota1/NetBeansProjects/Analyzer/lib/rubymethods.rb:32: syntax
    error, unexpected tIDENTIFIER, expecting tAMPER or '&'
    def split_apart(first, *splat, last)
    ^
    --
    Posted via http://www.ruby-forum.com/.
     
    Kerwin Franks, May 4, 2010
    #1
    1. Advertising

  2. 2010/5/4 Kerwin Franks <>:
    > Hello, i need some help here, when i run the following code i get the
    > error below. Why is that, according to the textbook it should work, also
    > looked it up in the cookbook, no luck there too. Thanks in advance.
    >
    > def split_apart(first, *splat, last)
    > =A0puts "first: #{first.inspect}, splat: #{splat.inspect}, " +
    > =A0 =A0 =A0 "last: #{last.inspect}"
    > =A0end
    >
    > split_apart(1,2)
    > split_apart(1, 2, 3)
    >
    > Error:
    >
    > /home/gribota1/NetBeansProjects/Analyzer/lib/rubymethods.rb:32: syntax
    > error, unexpected tIDENTIFIER, expecting tAMPER or '&'
    > def split_apart(first, *splat, last)
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ^


    10:49:13 test_2$ allruby -ce 'def s(a,*b,c) end'
    CYGWIN_NT-5.1 padrklemme1 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
    -e:1: syntax error, unexpected tIDENTIFIER, expecting tAMPER or '&'
    def s(a,*b,c) end
    ^
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin]
    Syntax OK
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java
    HotSpot(TM) Client VM 1.6.0_20) [x86-java]
    SyntaxError in -e:1: , unexpected tIDENTIFIER

    def s(a,*b,c) end
    ^
    11:16:32 test_2$

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 4, 2010
    #2
    1. Advertising

  3. Kerwin Franks

    Ruby Knight Guest

    >
    > def s(a,*b,c) end
    > ^
    > 11:16:32 test_2$
    >
    > Cheers
    >
    > robert



    Hi Robert,

    I tried putting end after the last brace but didn't work. I am assuming
    there is more to that ^ than just for illustrative purposes. Could you
    explain to me why my code is not working? THanks!

    --
    Posted via http://www.ruby-forum.com/.
     
    Ruby Knight, May 4, 2010
    #3
  4. Ruby Knight wrote:
    >> def s(a,*b,c) end
    >> ^
    >> 11:16:32 test_2$
    >>
    >> Cheers
    >>
    >> robert
    >>

    >
    >
    > Hi Robert,
    >
    > I tried putting end after the last brace but didn't work. I am assuming
    > there is more to that ^ than just for illustrative purposes. Could you
    > explain to me why my code is not working? THanks!
    >
    >


    In Ruby, formal parameters have a particular order:

    def s(required, default="default", *variable_args, &block)

    You cannot have a regular required parameter after an optional parameter
    (denoted by *). The only thing you can have is a block parameter
    (denoted by &) or nothing at all.

    -Justin
     
    Justin Collins, May 4, 2010
    #4
  5. Kerwin Franks

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, May 4, 2010 at 4:31 AM, Ruby Knight <>wrote:

    > >
    > > def s(a,*b,c) end
    > > ^
    > > 11:16:32 test_2$
    > >
    > > Cheers
    > >
    > > robert

    >
    >
    > Hi Robert,
    >
    > I tried putting end after the last brace but didn't work. I am assuming
    > there is more to that ^ than just for illustrative purposes. Could you
    > explain to me why my code is not working? THanks!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Robert is saying that it works in 1.9.1 (see how it says Syntax OK), but not
    1.8.7 (where it pulls up the same error you had)


    For 1.9, you can do something like this

    def split_apart( first , *splat )
    raise ArgumentError.new('wrong number of arguments (1 for 2)') if
    splat.size.zero?
    last = splat.pop
    puts "first: #{first.inspect}, splat: #{splat.inspect}, last:
    #{last.inspect}"
    end

    Though I probably wouldn't bother with the error myself, unless writing code
    for other people.

    This is similar to how Rails' find method works, it checks the last argument
    to see if it is a hash, then if it is, it pops it off (look at their example
    in the comments)

    http://github.com/rails/rails/blob/...ive_support/core_ext/array/extract_options.rb
     
    Josh Cheek, May 4, 2010
    #5
  6. Kerwin Franks

    Robert Dober Guest

    On Tue, May 4, 2010 at 11:55 AM, Justin Collins <> wrote:
    <snip>
    It is legal in 1.9 and illegal in 1.8
    HTH
    R.
     
    Robert Dober, May 4, 2010
    #6
  7. Ruby Knight wrote:
    > Hello, i need some help here, when i run the following code i get the
    > error below. Why is that, according to the textbook it should work, also
    > looked it up in the cookbook, no luck there too. Thanks in advance.
    >
    > def split_apart(first, *splat, last)


    What Robert is saying is that is valid syntax for ruby 1.9, but not for
    ruby 1.8.

    I don't know what "the textbook" and "the cookbook" are that you refer
    to, but they were probably written for 1.9.

    ruby 1.9 is a substantially different language to ruby 1.8, which you
    probably wouldn't expect from the "minor" version bump.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, May 4, 2010
    #7
  8. Hi --

    On Tue, 4 May 2010, Robert Dober wrote:

    > On Tue, May 4, 2010 at 11:55 AM, Justin Collins <> wrote:
    > <snip>
    > It is legal in 1.9 and illegal in 1.8


    Ditto in arrays as well as parameter lists. (Kind of interesting that
    1.8.7 also gives the useless literal error.)

    $ ruby -ve '[1,2,*[3,4],5]'
    ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-darwin9.8.0]
    -e:1: syntax error, unexpected ',', expecting ']'
    [1,2,*[3,4],5]
    ^
    -e:1: warning: useless use of a literal in void context

    $ ruby191 -ve 'p [1,2,*[3,4],5]'
    ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin9.8.0]
    [1, 2, 3, 4, 5]


    David

    --
    David A. Black, Senior Developer, Cyrus Innovation Inc.

    THE Ruby training with Black/Brown/McAnally
    COMPLEAT Coming to Chicago area, June 18-19, 2010!
    RUBYIST http://www.compleatrubyist.com
     
    David A. Black, May 4, 2010
    #8
  9. Kerwin Franks

    Ruby Knight Guest

    Thanks guys for all the input as well as the timely responses! I have to
    upgrade my Ruby version then, i thought i had the latest version
    installed but seems not.

    Regards


    --
    Posted via http://www.ruby-forum.com/.
     
    Ruby Knight, May 4, 2010
    #9
  10. Robert Dober wrote:
    > On Tue, May 4, 2010 at 11:55 AM, Justin Collins <> wrote:
    > <snip>
    > It is legal in 1.9 and illegal in 1.8
    > HTH
    > R.
    >
    >

    Hmmm...noted.

    -Justin
     
    Justin Collins, May 4, 2010
    #10
  11. Kerwin Franks

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, May 4, 2010 at 5:35 AM, Ruby Knight <>wrote:

    > Thanks guys for all the input as well as the timely responses! I have to
    > upgrade my Ruby version then, i thought i had the latest version
    > installed but seems not.
    >
    > Regards
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    > I've been pretty happy with rvm

    http://rvm.beginrescueend.com/
     
    Josh Cheek, May 4, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Berger

    Doing a splat within a C extension

    Daniel Berger, Nov 3, 2005, in forum: Ruby
    Replies:
    3
    Views:
    133
  2. Eero Saynatkari

    Splat, #to_ary and #to_a

    Eero Saynatkari, Sep 18, 2006, in forum: Ruby
    Replies:
    18
    Views:
    249
    Rick DeNatale
    Sep 20, 2006
  3. Adam Shelly

    redefining splat?

    Adam Shelly, Oct 2, 2006, in forum: Ruby
    Replies:
    10
    Views:
    371
    Martin Coxall
    Oct 3, 2006
  4. Trans

    '**' as hash splat?

    Trans, Oct 24, 2006, in forum: Ruby
    Replies:
    12
    Views:
    223
    Kalman Noel
    Oct 26, 2006
  5. Gary Wright
    Replies:
    3
    Views:
    119
    Macario Ortega
    Jun 3, 2009
Loading...

Share This Page