-1

The following simple code in a migration file in Ruby on Rails :

def change
create_table :articles do |t|
  t.string :title 
end

Can I understand it like that :

1: create_table is a ruby built-in ( or Ruby on Rails ? ) to create a table

2: :articles is the table name, so clear

3: This is the most important for me to illustrate in my brain. do |t| means to execute what is behind it ? this is mean do will add the table in the database, and gives it the value t ???

bbparis
  • 127
  • 5
  • This is nothing like unix pipes, if that's what you were asking. – Sergio Tulentsev Jun 01 '16 at 12:37
  • Hello, I know it is a simple question, but it is still hard for me to understand the role of pipe. I don't care about the rating of my profile on stackoverflow, I care about my knowledge; What is mean inside a Ruby on Rails code, with we use pipes, this is what I was asking for. Thanks for your negative reaction. – bbparis Jun 01 '16 at 12:45
  • @Sergio, NO, I'm just asking about the role of pipes inside Ruby code. Thanks for your comment – bbparis Jun 01 '16 at 12:46
  • If you truly do care about your knowledge, you shouldn't be getting piecemeal knowledge of very basic ruby syntax constructs from stackoverflow questions. You should be reading a book. – Sergio Tulentsev Jun 01 '16 at 12:56
  • you are right Sergio, I'm following now video learning ( a course for the beginner) about 260 videos. I thought I can come to here for any question. Anyway, not easy to understand all things in one time. – bbparis Jun 01 '16 at 12:58
  • Among those 260 videos, there must be several about blocks. Find those and watch. Should clear the concept for you. – Sergio Tulentsev Jun 01 '16 at 17:03
  • I'm at 20% now of the course, but so happy. Anyway he explained quickly about the do, and I'm still learning. I will not post here any future simple question, do you know a good community to help me understand these small issues ? Thanks again for your help Sergio! – bbparis Jun 01 '16 at 17:06

1 Answers1

1

What you see here is a block, one of ruby's most powerful language constructs. create_table is called with the first parameter being a symbol indicating the name of the table, and the second parameter being a block (the bits between do and end). Assuming this is Rails, you can read more about that method here. The pipes indicate that t is a block parameter, which in this case refers to the table being created.

This answer goes into more depth about what blocks are.

Community
  • 1
  • 1
Munyari
  • 85
  • 8