I'm trying to learn Ruby as well as Ruby on Rails right now. I'm following along with Learning Rails, 1st edition, but I'm having a hard time understanding some of the code.
I generally do work in C, C++, or Java, so Ruby is a pretty big change for me.
I'm currently stumped with the following block of code for a database migrator:
def self.up
create_table :entries do |t|
t.string :name
t.timestamps
end
end
Where is the t variable coming from? What does it actually represent? Is it sort of like the 'i' in a for(i=0;i<5;i++) statement?
Also, where is the :entries being defined at? (entries is the name of my controller, but how does this function know about that?)
When you call a method on an object, Ruby looks for the implementation of that method. It looks in the following places and uses the first implementation it finds: Methods from the object's singleton class (an unnamed class that only exists for that object)
Ruby is an open source, interpreted, object-oriented programming language created by Yukihiro Matsumoto, who chose the gemstone's name to suggest "a jewel of a language." Ruby is designed to be simple, complete, extensible, and portable.
What can Ruby be used for? The Ruby programming language is a highly portable general-purpose language that serves many purposes. Ruby is great for building desktop applications, static websites, data processing services, and even automation tools. It's used for web servers, DevOps, and web scraping and crawling.
As of Ruby 2.6, YARV remains the official interpreter technology used for Ruby. When you run a Ruby program, YARV translates the code to a limited instruction set that can run in the Ruby virtual machine (VM).
:entries
is a symbol literal, it's a literal value like 7
or "a string"
. There's nothing to define (incidentally, the function doesn't know about the name of your controller).
t
is the parameter to the block you've passed in to the create_tables
method. What you've written here is roughly analogous to something like:
void anonymous_block(Table *t) {
t->string("name");
t->timestamps();
}
...
create_table("entries", &anonymous_block);
in C++. create_table
calls your block and passes it a parameter, which you've named t
. I would suggest you get an introductory book for ruby as opposed to rails. I recommend Ruby For Rails by David A. Black.
I'll take the t
thing. The create_table
method is like a C function that takes a function pointer that takes one argument, the table definition object (forgive my non-existent C skills):
void entries_table_constructor(TableDef table_definition) {
table_def_add_string_column(table_definition, "name");
table_def_add_timestamps_column(table_definition);
}
create_table("entries", entries_table_constructor);
But in Ruby, the definition of the passed function can be done at the moment the create_table
method is called. So the bit between the do
and end
is like the entries_table_constructor
function, and the t
variable is like the table_definition
argument.
There is a big difference between function pointers in C and blocks in Ruby however. In Ruby, all the local variables outside the block are available inside the block:
a = 10
create_table :entries do |t|
puts a
...
end
Check out the yield
keyword in Ruby to see how to write your own methods like create_table
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With