slice returning multiple values

Discussion in 'Ruby' started by Joel VanderWerf, Apr 26, 2004.

  1. Fantasy for the day...

    str = "foo bar"
    x, y = str[/(foo) (bar)/, 1, 2]

    x # ==> "foo"
    y # ==> "bar"

    Oh, well, there is always this:

    (x, y), = str.scan(/(foo) (bar)/)
     
    Joel VanderWerf, Apr 26, 2004
    #1
    1. Advertising

  2. "Joel VanderWerf" <> schrieb im Newsbeitrag
    news:...
    >
    > Fantasy for the day...
    >
    > str = "foo bar"
    > x, y = str[/(foo) (bar)/, 1, 2]
    >
    > x # ==> "foo"
    > y # ==> "bar"
    >
    > Oh, well, there is always this:
    >
    > (x, y), = str.scan(/(foo) (bar)/)


    irb(main):007:0> x, y = str.match(/(foo) (bar)/).to_a[1..2]
    => ["foo", "bar"]

    irb(main):010:0> x, y = str.split /\s+/
    => ["foo", "bar"]
    irb(main):011:0> x
    => "foo"
    irb(main):012:0> y
    => "bar"
    irb(main):013:0>

    irb(main):013:0> x, y = str.scan /\w+/
    => ["foo", "bar"]

    irb(main):018:0> x, y, rest = str.split( /\s+/, 3 )
    => ["foo", "bar"]
    irb(main):019:0> rest
    => nil

    robert
     
    Robert Klemme, Apr 27, 2004
    #2
    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. Jorgen Gustafsson
    Replies:
    4
    Views:
    604
    Jorgen Gustafsson
    Dec 12, 2003
  2. Anthony
    Replies:
    4
    Views:
    603
    Anthony
    Aug 7, 2003
  3. Antoon Pardon

    slice notation as values?

    Antoon Pardon, Dec 9, 2005, in forum: Python
    Replies:
    23
    Views:
    723
    Antoon Pardon
    Dec 12, 2005
  4. Roland Hall

    VBScript function returning multiple values

    Roland Hall, Jan 30, 2005, in forum: ASP General
    Replies:
    17
    Views:
    518
    Roland Hall
    Feb 5, 2005
  5. J Krugman

    Returning multiple values

    J Krugman, Apr 11, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    171
    Bob Walton
    Apr 12, 2004
Loading...

Share This Page