Slice returning multiple values

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” vjoel@PATH.Berkeley.EDU schrieb im Newsbeitrag
news:408D404D.3070801@path.berkeley.edu…

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