Run code for each value in array

I want to run a code for each of a certain value in a array... I can show an example of what I need like this;

My_Garden = [Apple, Apple, Apple, Pumpkin, Pear];My_Pocket = Array.newfor each Apple in My_Garden My_Pocket.push(Apple)endFrom the example above I want it to work like that Push as many apples as their are in My_Garden to My_Pocket.

another example

My_Offenses = [Stealing, Stealing, Stealing]for each Stealing in My_Offenses getArrested()endIn this example I would get arrested 3 times, XD (Just an example)

Basically, I want to run a code x times, when x = the amount of a certain object.

I am new to Ruby didn't cover it in school, and rarely used in the work place so any help is greatly appreciated, thanks in advance
my_garden = [Apple, Apple, Apple, Pumpkin, Pear]


my_pocket = Array.new


my_garden.each do |fruit|


my_pocket.push(fruit)


end
Could you elaborate? How would the engine know which value to check for?
Are they objects or text strings?
Objects and integers are what i'm looking for. But yes, mainly Objects.
Something like this might work (syntax might be a bit wrong - I don't have Ace with me to test it):

my_garden = [Apple, Apple, Apple, Pumpkin, Pear]my_pocket = Array.newmy_garden.each do |fruit| my_pocket.push(fruit) if fruit.is_a?(Apple)endor this:
Code:
my_garden = [Apple, Apple, Apple, Pumpkin, Pear]my_pocket = Array.newmy_garden.each do |fruit|   if fruit.is_a?(Apple)      my_pocket.push(fruit)   endend
or this:

Code:
my_garden = [Apple, Apple, Apple, Pumpkin, Pear]my_pocket = Array.newmy_garden.select{|fruit| fruit.is_a?(Apple)}.each do |fruit|   my_pocket.push(fruit)end
The first two ways process everything in my_garden, then does something with it if it's an Apple (the first way is better if you only have one thing to do, the second if you want to do several things). The third way selects all the Apples from my_garden, then does something with them.Don't start variable names with capital letters (eg - My_Garden, My_Pocket). ALLCAPS is for constants, InitialCaps is for classes, all_lower is for variables.
You'd have to loop through each object using the 'each' method, and then check yourself what the object is. For object class you'd want to do something like:

fruit.is_a?(Apple)

And that would return true if that fruit in particular was an apple. I can really explain it very well right now because i'm typing this out on my phone. Perhaps someone else can pick up from here.
Seems like it should work but how do I make fruit = each apple.
Seems like it should work but how do I make fruit = each apple.
When you call

array.each do |object|endIt will run everything that is inside the do...end for each value of the array, the value can then be accessed with the variable defined within the pipes on the first line (object)if you only want to add to the pocket the objects that are Apples, you call the "is_a?" method of the object, like Shaz did.

Code:
#Declare a list of fruitsmy_garden = [Apple, Apple, Apple, Pumpkin, Pear]#Creates a new arraymy_pocket = Array.new#Loop through every fruit on the "my_garden" array, setting the fruit value to the variable "fruit"my_garden.each do |fruit|   #if the variable fruit is of the type Apple   if fruit.is_a?(Apple)      #add it to the "my_pocket" array.      my_pocket.push(fruit)   endend
Ohh I see, thank you all.
Alternatively:

my_pocket = my_garden.select { |fruit| fruit.is_a?(Apple) }More information here.

Original thread: https://forums.rpgmakerweb.com/threads/run-code-for-each-value-in-array.42040/