Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

can someone explain: each do?

Hi fellow scripters!

I have a question that i hope someone might want to help me out with and explain for me..
What does ".each do"  do?

i mean, lets say a line looks something like this:
Code:
@text.sort.each do |keys, values|

or like this:
Code:
$data_classes[actor.class_id].learnings.each do |skill|

Theese are just some random lines, but i wonder if someone maybe could explain it for me and maybe give me some examples so that i can understand it better.. :shock: 
I've been trying to figure out what it does for quite a while now and it's driving me nuts, so any help would be very much appreciated! :thumb:
Well, thanks for reading my post!

Over and out - Gando
 

khmp

Sponsor

The best I can do is relate what I think it is. I know its not the exact definition but I think you will understand .each a little better. There are classes in Ruby, almost always collections, that have some form of iteration built into them. The .each method is a way to iterate through the elements in these collections.

Example:
Code:
parts = ['toast', 'is', 'amazing']
parts.each do |part|
  p part
end

The word between the "pipes"(||) is used as a representative of the current element in the iteration. First run part will be toast, then is, then amazing. However changes made to the representative are not always reflected in the collection. Not always is indefinite isn't it? When do the changes take effect. It all depends on how good a shallow copy gets you. With simpler items you can easily make a duplicate but with larger more complex items copying becomes more difficult.

Example:
Code:
# When shallow copy works.
parts = ['toast', 'is', 'amazing']
parts.each do |part|
  part += 'er'
end
p parts # Prints ['toast', 'is', 'amazing']

# When shallow copy fails.
parts = [Sprite.new, Sprite.new, Sprite.new]
parts.each do |part|
  part.x += 50
end
p parts[0].x, parts[1].x, parts[2].x # 50 50 50

Just keep that in mind when using .each. If you do want to manipulate an array's innards but are unsure on the copy situation you can either "for i in 0...array.size" or specifically for Array objects use .each_index do |i|

Example:
Code:
parts = ['toast', 'is', 'amazing']
parts.each_index do |i|
  parts[i] += 'er'
end
p parts # Prints ['toaster', 'iser', 'amazinger']

For hashes you have a couple approaches. First a little quickie for Hashes. Hashes use {} instead of [] for Arrays. Hashes use a key to reference a piece of data stored within it. Like the index for an Array. But with a Hash that key could be anything. An Integer, String, or even a Window. Its really up to you how you want to attack your data storage problem.

To iterate through a hash with .each you need to define between the pipes two values. One for the key and one for the data referenced by the key.

Example:
Code:
parts = { 0 => 'toast', 1 => 'is', 2 => 'amazing' }
parts.each do |key, value|
  p key, value
end
p parts # Prints 0 toast, 1 is, 2 amazing

That was a little deceptive. When key value pairs are entered into Hashes, regardless of the order you put them into the hash, it does not guarantee that the data will stay in that order. The last item you throw in a Hash could be the first item encountered when iterating. Point is iterating through hashes is guess work.

Like I said above you can iterate through Hashes a bunch a different ways. each, each_pair, each_key, each_value. each_pair is the synonym for each. It works the same way as each it just takes twice as much time to type... Sometimes when dealing with hashes you might only care about the key and not the value. For this we use .each_key

Code:
parts = { 0 => 'toast', 1 => 'is', 2 => 'amazing' }
parts.each_key do |key|
  p key
end
p parts # Prints 0, 1, 2

Likewise there is a method for just the values of a hash.

Code:
parts = { 0 => 'toast', 1 => 'is', 2 => 'amazing' }
parts.each_value do |value|
  p value
end
p parts # Prints 'toast', 'is', 'amazing'

As always, good luck with it Gando! :thumb:
 
So.. much.... text... brain... malfunctioning..! :crazy:
nah, just kidding with you, this is great! Exactly what i needed. :thumb:
I'll read through this a couple of times and maybe i'll understand it better after that.
It feels like your my personal tutor, since you've helped me by explaining so many things.^^

Well, once again, thanks for all the help!
Btw, i like your new Avatar, did you make it yourself?  :smile:

Over and out - Gando
 

khmp

Sponsor

Gando":295st67y said:
Btw, i like your new Avatar, did you make it yourself?  :smile:

Over and out - Gando

I copied it directly from a desktop graphic. And then added some blue in the back. I thought it was a clever stab at AbyssalLord's Ichigo after he changed his user title to the opposite of mine.

Anyway glad to help.
 
khmp":37opx35n said:
Gando":37opx35n said:
Btw, i like your new Avatar, did you make it yourself?  :smile:

Over and out - Gando

I copied it directly from a desktop graphic. And then added some blue in the back. I thought it was a clever stab at AbyssalLord's Ichigo after he changed his user title to the opposite of mine.

Anyway glad to help.

You took the optimistic side, so I took the pesimistic side...lol.  Anyway, Grimmjow is my second favorite character (actually, probably on the same level as Ichigo), but I digress.

Now, so as not to post someting completely irrelevant to the topic...

Your explanation is really helpful.  Nice.

Ok, so that wasn't much, but it was something.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top