1

I am following a tutorial.

arr = [["cow", "moo"], ["duck", "quack"]]
bucket = arr[0]
bucket.each_with_index do |kv, i|
  k, v = kv
  puts k
  puts v
end

I understand that when it goes through arr[0], it is first kv = cow and i = 0, and then kv = moo and i = 1. How does k, v = kv make sense? To me, it seems that k = kv and v = kv, but that isn't the case. I don't understand the purpose.

5
  • Not clear what you mean by k = kv and v = kv. Commented Jun 8, 2016 at 20:42
  • I think he is confused by the one line multiple variable assignment. Hope my answer clarifies it a bit. Commented Jun 8, 2016 at 20:44
  • I suspect you should have had arr.each_with_index .... Commented Jun 8, 2016 at 20:53
  • 1
    That tutorial by Zed Shaw has a heavy Python accent to it. There's a lot of conventions employed there that go against most Ruby styleguides, like having upper case letters in variable names, using the module or class name when declaring methods, and having get_ in the method name. Commented Jun 8, 2016 at 21:01
  • Plus, wouldn't a ruby hash be better than the arrays? Commented Jun 8, 2016 at 21:03

2 Answers 2

1

Basically the line k, v = kv means that you are assigning kv to k or k= kv

comma separated assignment is just another way of assigning multiple variables in one line, it's just syntactic sugar.

Try messing around with your code and do something like this for example:

    arr = [["cow", "moo"], ["duck", "quack"]]
    bucket = arr[0]
    bucket.each_with_index do |kv, i|
      k, v = kv, i
      puts "k is #{k} and v is #{v}"
    end
   # k is cow and v is 0
   # k is moo and v is 1
Sign up to request clarification or add additional context in comments.

7 Comments

that's actually a good question... Could you provide me with the link you got that code from ?
each_with_index do |(k, v), i| avoids having to add the break-out line.
It's linked up top click "tutorial". It's in the method called def Dict.get_slot on the page
@tadman that's interesting. could you explain the block |(k,v),i| it's the first time I see someone doing that :)
@tadman There is a method that calls the code I reference, that method specifically wants to return v. So it isn't actually nil when it's running. I just don't get why.
|
0

What's happening here is it's breaking out the key/value pair variable kv into two separate variables k and v. You'll need to adjust your code to look like this:

arr = [["cow", "moo"], ["duck", "quack"]]
arr.each_with_index do |kv, i|
  k, v = kv
  puts k
  puts v
end

What's happening here is demonstrated by this:

kv = [ :key, :value ]

# Assign k to the first value, v to the second in kv
k, v = kv

k
# => :key
v
# => :value

A less messy way of doing this break-out is this:

arr.each_with_index do |(k, v), i|
  # ... Use k and v normally
end

That notation allows you to expand the array into separate variables in advance of using them. It's a trick that comes in handy when dealing with value pairs of this sort.

It's worth noting you wouldn't have to do this if you didn't use each_with_index, as each is sufficient here since that index value is never used:

each do |k, v|
  # ...
end

5 Comments

I suspect that is what the example should have been, but that is not what the OP has.
Beware of what bucket is. It is different from arr.
@sawa I forgot to correct that in my example. That really shouldn't be there in the first place, so I've removed it. Seems you've made a similar observation.
Ok, I tried that out and it made more sense. Thanks man, I just couldn't find any example like that in the documentation.
There's a lot of examples in the documentation, but to really get a sense of how it all comes together you might need a better introduction book to Ruby. The original "Pickaxe book" is now free and worth a read through. I found it tremendously valuable when I was learning Ruby.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.