Why splat variable couldn't take in the below two code the "range" -> (1..8)? means why a gives []? *a,b = (1..8) #=> 1..8 b #=> 1..8 a #=> [] means why b gives []? a,*b = (1..8) #=> 1..8 a #=> 1..8 b #=> []
on 2013-02-23 06:52
on 2013-02-23 09:52
On Sat, 23 Feb 2013 07:08:05 +0100, Matthew Kerwin
<lists@ruby-forum.com> wrote:
> Splat is an array, Rang is not an array.
Matthew, what? This doesn't even make sense.
on 2013-02-23 10:07
On Saturday 23 February 2013 Love U Ruby wrote > #=> [] > -- > Posted via http://www.ruby-forum.com/. In both case, the right hand side of the assignment is a single object. When this happens, the object can be assigned to only one of the variables on the left hand side of the assignment. In your first example, the * before a tells that a can contain any number of values (including no values). So ruby assign the only availlable value (the range) to b and assign an empty array to a. In the second case, it's the opposite. Arrays and objects responding to #to_ary are an exception to the rule, as they are automatically distributed among the variables: *a,b = (1..8).to_a #=> [1,2,3,4,5,6,7,8] b #=> 8 a #=> [1,2,3,4,5,6,7] You can obtain what you want without explicitly converting the range to an array using the splat operator: *a,b=*(1..8) #=> [1,2,3,4,5,6,7,8] b #=> 8 a #=> [1,2,3,4,5,6,7] Stefano
on 2013-02-23 10:41
On Sat, Feb 23, 2013 at 9:52 AM, Bartosz Dziewoński <matma.rex@gmail.com> wrote: > On Sat, 23 Feb 2013 07:08:05 +0100, Matthew Kerwin <lists@ruby-forum.com> > wrote: > >> Splat is an array, Rang is not an array. > > > Matthew, what? This doesn't even make sense. It does, because irb(main):004:0> Range.instance_method :to_ary NameError: undefined method `to_ary' for class `Range' from (irb):4:in `instance_method' from (irb):4 from /usr/bin/irb:12:in `<main>' and, as our friend would have learned a few days back if he followed the advice to use set_trace_func to learn what's going on irb(main):015:0> o = Object.new => #<Object:0x802a7a80> irb(main):016:0> def o.to_ary; [1,2,3,4] end => nil irb(main):017:0> a, *b, c = o => #<Object:0x802a7a80> irb(main):018:0> a => 1 irb(main):019:0> b => [2, 3] irb(main):020:0> c => 4 Cheers robert
on 2013-02-23 13:45
On 23 February 2013 18:52, Bartosz Dziewoński <matma.rex@gmail.com> wrote: > On Sat, 23 Feb 2013 07:08:05 +0100, Matthew Kerwin <lists@ruby-forum.com> > wrote: > > Splat is an array, Rang is not an array. >> > > Matthew, what? This doesn't even make sense. > I know, I know. I wasn't paying attention while typing and didn't realise how bad it was. I edited the post immediately on the forum interface, but apparently the mailing list version got sent first. I think it now says something like "Splat is an array operation, Range is not an array."
on 2013-02-23 14:03
On Sat, 23 Feb 2013 13:44:25 +0100, Matthew Kerwin <matthew@kerwin.net.au> wrote: > I know, I know. I wasn't paying attention while typing and didn't realise > how bad it was. I edited the post immediately on the forum interface, but > apparently the mailing list version got sent first. > > I think it now says something like "Splat is an array operation, Range is > not an array." But you can splat a range? irb(main):004:0> a, b, c = *0..2 => [0, 1, 2] irb(main):005:0> a => 0 irb(main):006:0> b => 1 irb(main):007:0> c => 2 Are we misunderstanding each other?
on 2013-02-23 15:31
On Sat, Feb 23, 2013 at 2:00 PM, Bartosz Dziewoński <matma.rex@gmail.com> wrote: > > > Are we misunderstanding each other? You created an explicit implicit Array conversion by using "*" on the right side. The point Matthew and I were trying to make is that you cannot do the same with a range without the splat operator on the right side: irb(main):004:0> a, b, c = 0..2 => 0..2 irb(main):005:0> a => 0..2 irb(main):006:0> b => nil irb(main):007:0> c => nil Whereas with an Array you can: irb(main):012:0> a, b, c = [0, 1, 2] => [0, 1, 2] irb(main):013:0> a => 0 irb(main):014:0> b => 1 irb(main):015:0> c => 2 Cheers robert
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.