Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > slice returning multiple values

Reply
Thread Tools

slice returning multiple values

 
 
Joel VanderWerf
Guest
Posts: n/a
 
      04-26-2004

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)/)


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-27-2004

"Joel VanderWerf" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
slice notation as values? Antoon Pardon Python 23 12-12-2005 11:25 AM
VBScript function returning multiple values Roland Hall ASP General 17 02-05-2005 01:42 AM
Returning multiple values J Krugman Perl Misc 10 04-11-2004 11:37 PM
Comparing values in 2 textfiles and returning the missing values Jorgen Gustafsson Perl 4 12-12-2003 09:09 AM
Realloc() with 2D arrays; returning multiple values Anthony C Programming 4 08-07-2003 01:51 AM



Advertisments