1

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
deceze
  • 510,633
  • 85
  • 743
  • 889
rubygirl
  • 73
  • 1
  • 5

1 Answers1

2

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.

deceze
  • 510,633
  • 85
  • 743
  • 889