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
So before we go any further, let's take a quick look at a few gotchas when it comes to blocks
and they're surrounding variables.
I just love gotchas.
Here's the first one.
We've seen this block with a number block parameter.
It prints the number three times.
Sure, if we run this, we get three lines of howdy.
Now, what if we also wanted to print a name like we want to say howdy mo?
We could define a name variable inside the block and use it.
Sure, I'll just put it right up here.
Name equals mo.
And then we'll just say howdy mo.
Just like that.
And of course, if we run it, now we've got mo's name in the output.
Now you might think you could access the variable after the block runs.
Yeah, let's just down here, we'll just do a puts of name.
That's the name of the variable.
If we run this, we get an error.
It says undefined local variable or method name.
Well, why is that?
Well, variables that show up inside of the block only here are local to the block.
They're like local variables.
They're only in scope while the block is running or within this doing end.
So when we try to reference name outside of that block, it's not defined because once
the block is run, this name variable has evaporated.
So then what if the name was already a variable defined before the block?
Well, let's give that a try.
Just up here before the block, we'll set the name to something like curly.
Now if we run it, notice it prints out mo at the bottom.
That's that puts line here that's printing the name.
So we don't get an undefined variable.
It knows what name is.
And in fact, the value in name is mo.
Well, we set name to curly up here.
But then when the block ran, it set the name variable to mo.
And that's why we're seeing mo printed out outside of the block.
So anytime you have a variable defined before the block and it has the same name.
It's the same name variable inside of the block.
They're the same variable.
So if you change it inside of the block, well, it's changed when we reference it outside
of the block as well.
In other words, the block can change the value of local variables and it's surrounding scope.
But there are two exceptions to this.
We have a number block parameter.
What happens if we have a number variable defined before the block?
Yeah, let's just do that here.
We'll have number equal to say 100.
So we've got a variable here called number.
And we've got a block parameter called number.
And then we're printing out that number inside of the block.
So in the same way that name was shared, you might expect a number to be shared as well.
But let's go ahead and print that out down here.
Puts number.
If we run this, notice that the number that gets printed is 100 right here.
Well, we set number to 100 at the top.
But then remember when the block runs, this time's method is going to assign values to the
number parameter.
It's going to be 1, 2, and 3, which we saw printed inside of the block.
Actually it's 0, 1, and 2 in this case because it's the time's method.
But then when we print number out at the end, it's back to 100 again.
So even though it seems like this would get overwritten right here, the number variable
is preserved outside of the block.
So the takeaway here is block parameters, or the names of block parameters here, aren't
shared with variables of the same name in the surrounding scope.
The number variable here and the number block parameter here are different variables.
This is actually great news.
This means that we can use any name we want for a block parameter without worrying that
it will clobber or overwrite a variable of the same name in this surrounding scope.
Yeah, you can imagine all sorts of variables being defined before this block.
And number might be a very common one to use.
So even though you called this block parameter number here, you don't have to worry about
it clobbering any of those variables.
So let's look at one last example.
Suppose we're doing some calculations and we want to store it in a temp variable before
the block.
Like I don't know, pi divided by 4.
Yeah, that sounds like a fun calculation.
Then inside the block, we have a temp variable for storing the actual temperature.
Temperature in this case, let's say 98.6.
And then we can print out what the temperature is.
So this would be like how do you know is 98.6 degrees.
Right.
Now what happens if we reference temp outside the block?
So we print temp there.
Let's run it.
Well, temp is 98.6.
So we know it was 98.6 inside of the block because we see the print outs here.
Howdymo is 98.6.
Up here it was set to a value other than 98.6.
So we know that these are shared variables.
It's got the same name here as it does here.
And this isn't a block parameter.
So it's the same variable.
So we've overwritten the temp variable inside of our block.
Now sometimes you don't want this to happen.
Say for example, we want to keep this temp variable, but also keep this local variable
inside of the block, but not have them be the same thing.
And there's a really easy way to set that up.
Inside of our block parameter list, we can use a semicolon.
And then we just give it the name of any parameter or variable that we want to protect.
In this case, we're going to say temp.
And by doing this, we're telling Ruby, hey, I know there's a temp variable out here, or
there's a variable called temp out here.
And I'm going to have a variable called temp inside of my block as well.
But I want this temp to be a block local variable.
In other words, I want them to be different variables.
I don't want this one to overwrite the one in this rounding scope.
So now if I save that and print it out, you notice that temp is printed here.
Is the value of that math pi divided by four rather than 98.6, which overwrote the variable
there.
So again, you just use a semicolon.
And then you can list any parameters or any variable names, I should say, after that,
that should be protected inside of the block.
So the takeaway here is the variables listed after the semicolon aren't considered block
parameters.
They don't get assigned values when the block is executed.
Think of them as reserved variable names for use inside the block.
So here's a quick recap.
Variables that appear only inside the block are local to the block.
They are not accessible outside the block.
Variables defined outside the block can be shared inside the block.
The block can change the value of local variables in its surrounding scope.
Block parameters are always local to the block, even if they have the same name as the local
variable in the surrounding scope.
Lastly, you can declare block local variables by putting them after a semicolon in the
blocks parameter list.
This way, they're variable outside the block with the same name is protected from change.
Now don't let all this throw you.
It's just good to keep in mind how blocks interact with their surrounding scope.
And often when I get some weird behavior with the block, it's because I've got some variable
mixed up somewhere.
So feel free to experiment with these rules on your own.
It's a great way to practice and learn.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.