Himmators
Himmators

Reputation: 15006

What is |tag| in ruby?

I'm looking at a rails-project and trying to understand this code:

tags.each { |tag| markup << tag(:meta, tag) }

The part markup << tag(:meta, tag) adds an item to the array markup with the content of tag. but what does |tag| do?

Upvotes: 1

Views: 58

Answers (2)

Florent Ferry
Florent Ferry

Reputation: 1387

|tag| represents an element of the tags array.

The each operator returns all elements of an array or a hash. Inside its block, you execute code for each element in tags, and each element is passed to the block as a variable tag.

Upvotes: 2

jmccure
jmccure

Reputation: 1249

|tag| allows you to assign a name of tag to each item in the collection tags so you can use it within the block

You can put anything there, it would be equivalent to: tags.each { |x| markup << tag(:meta, x) }

Upvotes: 0

Related Questions