I`m new to rails, so that question may be stupid.
I have seen a lot of code like this
method do |x|
x.something
x.blabla
end
For example some snippet from migrate
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
What happens here ? |t| is passed to create_table method or ?
I can`t fugure out
The
|x|is a parameter being passed to the block. It is a feature of Ruby, not specific to Ruby on Rails.Here’s a very contrived example of how you might implement a function which accepts a block:
Effectively, you’re invoking
eachand passing it two things: An array (my_array) and a block of code to execute. Internallyeachloops over each item in the array and invokes the block on that item. The block receives a single parameter,|i|, which is populated byeachwhen it calls proc:proc.call(items[i]).