Absolute newbie at Ruby. I don't really understand the |animal|
section. Can someone please explain it to me?
ark = ["Cat", "dog", "pig", "goat"]
ark.each do |animal|
puts animal
end
Absolute newbie at Ruby. I don't really understand the |animal|
section. Can someone please explain it to me?
ark = ["Cat", "dog", "pig", "goat"]
ark.each do |animal|
puts animal
end
In other languages the syntax may look something like this:
ark = ["Cat", "dog", "pig", "goat"];
ark.each(function (animal) { puts animal; });
Does that clear it up? It's the syntax for an anonymous function. If you're not familiar with that concept, how about this?
function putAnimal(animal) {
puts animal;
}
ark = ["Cat", "dog", "pig", "goat"];
ark.each(putAnimal);
|animal|
is the argument list for the anonymous function. Very roughly speaking, Ruby's syntax for the common function (arg) { ... }
is do |arg| ... end
.