Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Okay, let's jump right in and write some basic blocks.
Yeah, starting with some basic blocks syntax and style,
we'll give us a good foundation to build on.
So we're using this sublime text editor here,
and I've got a new file open called block underscore basics.rb.
We're just going to use this as a little scratch pad
for playing around with some blocks here.
So the first question is, what is a block?
Well, the block is simply a chunk of code
that goes between do and end.
So we can put any ruby code we want between do and end here.
I'm just going to print out echo just like that.
So this is the block, but we can't run the block directly.
Instead, to get this block of code to run,
we need to attach or associate it with a method call.
Now ruby has a number of methods that will take blocks
and run them.
The method we're going to use is the time method.
And we call it on a number.
I'm going to call it on the number three, three times.
That's the method name.
And then we attach the block by simply putting it
after the method call there.
Now to run the block inside a sublime text,
I'm just going to use command to be on a Mac
or you can use control be on a PC.
And we get our output down here.
We see it printed echo three times, no surprise.
Don't worry about this little finished thing down here.
That's just the blind text telling us
that it finished running the code.
So when the time's method runs, it turns around
and runs the code in the block three times in this case.
The block kind of looks like a parameter to the method.
Well, it does look like a parameter,
but it's actually not a method parameter.
And we can make that more explicit after the time's method.
We can use parentheses where we would normally put
method parameters inside of those parentheses.
And we see that the block actually comes after those parentheses.
So it's not a parameter to the method.
In fact, we run this.
We get exactly the same thing.
So instead, the block is simply associated with
or attached to the method call in this case
that time's method call.
That don't worry about this. We're going to explore this throughout
the entire course.
Yeah, and we'll see a case where the method actually takes a parameter in just a minute.
So the convention is to use this do-in-in style
when you have a multi-line block.
Now, we've just got a single line block here.
We've just got a single line of Ruby code inside of this block.
When you have a single line block like this,
instead of using do-in-in, you can use curly braces.
I'm just going to bring this line to code back up here,
put it on the same line, get rid of the end,
and just use curly braces to surround the block.
So here we've got our method call,
and then here's the block after the method call.
Just like we did with doing in, this time it's just on a single line.
If we run this, we get the same thing.
So let's look at blocks in a different scenario.
Let's say we have a really simple order class like this one.
An order has a customer's email and total amount,
which we initialize, and then we wrote our own 2S method,
which just prints that out that information.
The first thing we want to do is create five example orders
and put them in an array,
and we can use the Times method to do that fairly quickly.
Sure, we'll just do it right down here.
I'm just going to add a little bit of space to give us a little bit of room.
You want to put them into an array,
so let's start with an empty Ruby array.
Just like that, we'll just call it orders.
Let's do five orders.
Okay, so instead of using three times where you use five times,
and then we're going to give it a block.
Block begins with do-ins with end and inside of there,
we can put any Ruby code we want.
In this case, we want to create an order
and append it to this order's array.
So we're going to take the orders array,
use the Append operator like that,
and then create a new order.
After passing the customer's email,
which I'll just use customer at example.
Dot com, and then we'll give the order total of say $10.
Didn't down below here, we want to print out these orders,
and I'm just going to use puts on orders.
If you call puts on an array,
it just turns around, it loops through that array for you,
and then it gets the string returned by 2S,
which we have one of those,
and then it will go ahead and print that out.
So if we save that and run it,
we see that we have five orders.
They have the same customers' email and total.
You know what we'd like instead,
we'd like each order to be kind of unique.
Maybe have a unique email and a unique total amount.
Sure, let's just return to our block basics file here,
and see how we can change that around.
So I'm going to start by changing this back around to do an end.
It's going to make it a little easier to demonstrate,
like that.
Now it turns out that the times method will pass
the iteration number to the block,
and we can pick that up in what's called a block parameter,
and we put block parameters after do,
and they go inside of vertical bars.
So I'm going to call the parameter number.
It's just a variable, so we can call it whatever we want,
but I'll call it number here.
Then inside of the block,
we can reference that variable number
to print out the number of the iteration
as this block is run.
Go ahead and run that.
And we see that the time method starts with zero
and counts zero, one and two.
So each time it calls the block,
it passes the number in that block parameter called number,
and then we just print it out.
So just to demonstrate that that's a variable,
we can actually change number to just end.
Sure, in fact, with really short blocks like this,
it's very common just to use single letter block parameter.
So as long as we call it end as a block parameter there,
and reference it with the same name, it works just the same.
Now we'll change it back to the single line form,
and we'll just take the do,
that'll be a brace there.
We'll move this back up to that line,
and we'll get rid of end, put our curly brace there.
So you see that the block parameter just goes inside of the block
to the right hand side of this opening curly brace.
So we've got our method called here,
and we've got our block here.
All right, so let's go back to our orders,
and we can now apply this to creating our orders
to have unique emails and totals.
Sure, I'll just jump back over to our order.rb file.
And down where we're creating the orders
with our time method,
we know that it's going to give us the number of the order,
in fact in this case, I'm going to go ahead and use N
just like we did before.
So there's our block parameter.
Now when we're creating the customer,
or the order in this case with the customers email,
I'll just interpolate that number there,
that way we'll get like customer 1, 2, 3, and so on.
And in fact, we'll change the totals too.
I'll take that number whatever it is,
and we'll just multiply it by 10.
We say that and run it.
We see we now have different customer emails.
They start with zero and go all the way up to four,
and then total starting with zero going up to 40.
So our orders are all different now.
There's zero through 40, but it would be better
if they were like 10 through 50 and customers 1 through 5.
You don't really want to be customer zero.
So maybe we could change that.
Yeah, the time method's not going to work in this case.
So we're going to need to use another method.
So let's go back to our scratch pad and play around with it.
So the time method won't work for us
because it starts counting at zero,
but there's another method we can use, the up to method.
So we want to start with one, and we want to go up to a different number.
In this case, we want to go 1 through 3.
So 1 up to 3.
Now notice that 3 is a method parameter.
It's in parentheses here.
It's part of this method call.
The block comes after the method call,
which includes the parameter.
So there's the method call and there's the block.
It's just important to remember that the parentheses go before the block,
which is all the way on the right hand side.
In other words, the code block is not a method parameter.
So if we go ahead and run this,
what we see that we iterate from 1 to 3,
which is just what we want.
All right, so now we know how to go back to our orders
and create orders 1 through 5.
Sure, we'll jump over there.
Instead of using the time method here,
we're just going to change this from 1.
We're going to go up to 5 in this case.
We've already got the block parameter.
The block itself doesn't change at all.
It's just like when we're using the time method,
but we know that the up to method is going to give us the numbers
sequentially as we want.
If we run that, we get customer 1 through customer 5,
and now our total is go from 10 to 50.
Perfect.
So so far, we've seen three forms of blocks.
We started with a method call.
In this case, the time is method on a number.
Blocks cannot be run on their own.
They must be associated with a method.
Do marks the start of the block and in marks while the end.
Inside the block, we can put any Ruby code we want.
Here, the time method calls the block three times.
Each time, printing out echo.
For simple blocks like this, we saw how we can put them all on one line.
When we do that, instead of do an end, we use curly braces.
We also looked at how blocks can take parameters.
A block parameter is simply a local variable that sits between two vertical bars,
which some people call pipes or go posts.
When the block is called,
the value of the parameter is filled in by the method.
In the case of the time's method, each time it calls the block,
it passes the block the current iteration number zero through two,
which then gets assigned to the number parameter.
We then use the number parameter in the block to print the number.
So you can think of a block as being like an anonymous method.
It encapsulates a chunk of code and can take parameters.
We can also write this block on one line again,
curly braces in place of doing end.
Finally, we call the method that itself takes a parameter.
In this case, we called up to and passed it for as the limit,
which is the method parameter.
Then comes the associated block after the method parameters.
The block parameter stays the same as with time,
since up to also passes the current iteration number to the block.
So we get four echoes.
Again, we could write this block on a single line.
Just make sure that due and end are curly braces come after the parentheses for the method parameters.
Because remember, the associated code block is not a method parameter.
You'll frequently see single line blocks simplified further by using a single character
as the name of the block parameter.
Remember the name is arbitrary, so we can replace number with n,
and that gives us an elegant line of code.
So now it's your turn to write some basic blocks in the exercise.
And in the next module, we'll use blocks with another built in iterator method called each.
Now you'll see each use all over the place in Ruby and Rails code.
So come on back.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.