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 now that we have a good foundation in place for the remainder of the course, we're going
to examine a number of use cases where blocks can be used to solve problems elegantly.
I'm going to look at some very zig-a-files and you'll see some reoccurring patterns that
you can then hopefully apply to your own code.
So here's our first use case.
Programmers often need to measure how much time some arbitrary code takes to run.
I can't tell you how many times I've written code just like this.
You start by getting the current time and you set it to some variable like start time.
Then you run some arbitrary amount of code.
In this case, we've just simulated some code by sleeping for half a second.
And then you calculate the elapsed time, which is the current time minus whatever the
start time was, and then you print out, hey, this chunk of code took so many seconds.
So you're basically wrapping or bracketing this code here with some boilerplate code
getting the start time and calculating and printing the elapsed time.
And you end up with lots of duplicated or boilerplate code around any code that you want
to measure.
So how can we clean up the duplication here?
Well one way is to encapsulate it in a method that runs some chunk of code in the middle.
Here's the chunk of code we want to run in the middle, and here's the code we want
to bracket it with.
So let's write a method to do that.
Which is to find it up top here.
I'm going to call the method time it.
Which of the method do?
Well, first it needs to do this step, calculate the start time.
And then at the end it needs to do this step here, calculate the elapsed time and print
it out.
And then right in the middle here we needed to run our arbitrary code.
We know how to do that.
We just call yield.
Now down here to run this chunk of code, we call time it, that's the new of our method.
And we put the code inside of block, I'll use do end here.
So we're basically bracketing the code that varies, which is this code, with the code
that doesn't vary at all.
It never changes.
So we can use this with any block we want.
If we print this out now, it says it took 0.5 seconds.
And of course we can use this method with any block of code.
So we could call time it anywhere else in our program or our application.
And we know that it's going to be bracketed with the appropriate code here.
Now it's often handy to have some sort of label or description about the thing that you're
timing.
So you might want to pass in a parameter here like, oh, sleepy code, for example.
Well that's just a method parameter.
We capture it in a method parameter appear.
We'll just call it label.
And then we'll just print out whatever the label is to a county seconds.
We run that.
It says sleepy code took half a second.
This technique of running the code in the middle of some other code is a very common
pattern.
We're referred to as execute around because we're executing around the code in the block.
Next up, let's suppose we have this sensor class that has methods for returning the water
temperature and level.
We just made them random numbers just to make it more interesting.
And then let's say we are checking the sensor regularly and we need to generate an audit
trail.
Yes, we have this code down here.
We're checking the water temperature.
What do we do?
Well we ask the sensor for its temperature if it's less than 150.
We run that expression.
We assign the result to a variable called result.
If the result is true, then we print okay.
The water temperature is fine.
Otherwise, we print out failed.
We also do something very similar with the water level.
Yeah, we just ask the sensor for its level.
If it's greater than three.
So we assign that to the result.
If it is greater than three, well then we print okay.
That's a valid water level.
Otherwise, we print fail.
Now when we run it, depending on the random numbers we get,
we'll get different output.
So look like the water temperature was okay, but the water level failed.
We run it again.
We'll get different output depending on the random numbers.
So what works, but the problem is we have a lot of code duplication here.
The real meat of it is just calling sensor temperature here to check the temperature and
calling sensor level here to check the level.
And everything around these bits of code, well they're just checking code.
And they don't change depending on what we're checking with the sensor.
And we need to separate the boilerplate checking code from what is being checked.
So let's write a method that encapsulates that checking stuff for us.
I'm going to do it right up here, call the method with checking.
And we're going to have this take a description.
And then what's the method going to do?
Well the first part is printing this out.
So I'm just going to take this out of here.
And we're going to say checking.
And then we're going to interpolate in the description here.
All right.
We also need to do this if checking part.
That's boilerplate code.
So let's take that out of there.
All right.
Now in the middle of these two, we need to run the actual checking code.
To do that, we're going to use a yield.
That's going to yield to the block.
But we need to make sure to capture the result.
What the block returns to us.
So I'm going to sign it to a variable here called result, which then we check right here.
So now down here, we can remove this part.
And we can just call with checking.
We're checking the temperature.
Passing the eastering we want there.
Then give it the block.
The block simply makes the comparison on temperature in the value of 150.
And remember that the value of the last expression of the value in the block is passed
back to the method as the value of yield.
So whatever this returns, true or false, will be the value of result here, which then
we can just go ahead and check as we did before.
In the same way, we can go ahead and change this water level stuff to be with checking.
This is going to be the level.
We take all this boilerplate code out here.
Just check the sensor level is greater than 3 and get rid of all this duplicated code.
So now if we run it, where we get the same output, depending on the random numbers there.
But the really cool part of this is this is nice, tidy, checking code here.
And we've got all this boilerplate output stuff up in this with checking method.
So again, we've separated concerns here.
This method isn't concerned with what is being checked.
It just knows to look at the result and print out a pass or a fail status.
The block itself has the role of actually doing the checking.
This makes it really easy to change the checking code.
We could put the results in a database.
We could email somebody at midnight.
If the sensor fails, we could post it to a web server.
Sure, it's all encapsulated in this method.
We would just change what happens right here.
And method all can also be used with an arbitrary block.
Like we could check to see if we're happy if it's Saturday.
Sure, this is our happiness level.
And we just give it a block.
We could just say time dot now to see if it's Saturday and Ruby would just say time dot
now dot Saturday, question mark which returns to her false.
So we run that.
Well, unfortunately it's not Saturday, but we're still pretty happy.
You'll use this same basic pattern.
And each time you want to run a chunk of code before and after some code in the middle
that varies.
So basically you're executing around a block of code.
We're going to riff on this pattern with some more examples in the next module.
And before and after some code.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.