Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
At its most basic, a block is statements of code that are grouped together. The statements can be grouped inside of curly braces or between the do
and end
keywords.
Blocks and Return Values
Blocks must use the implicit return format in Ruby.
A Note on Convention
Conventionally, most Ruby programmers distinguish between the two formats the following way:
-
Multi line blocks are usually written using the
do
andend
format.-
Single line blocks are usually written using curly braces
{}
.
-
Single line blocks are usually written using curly braces
Code Samples
A code block using the do
and end
syntax:
3.times do
puts "Hello world!"
puts "This is the second line."
true
end
A code block using curly braces:
3.times { puts "Hello world!" }
[MUSIC]
0:00
[SOUND] You may have seen a block before
in Ruby code, they're really common.
0:04
At its most basic, a block is a statement
of code that is grouped together.
0:09
The statements can be grouped
inside of curly braces, or
0:15
between the do and end keywords.
0:18
Blocks are actually a piece
of syntax in Ruby and
0:21
not explicitly an object
like everything else.
0:24
Blocks can be a little confusing at first,
but they're actually really easy.
0:28
Let's take a look at some
blocks now using workspaces.
0:32
Okay, so
I have launched a new Ruby workspace.
0:36
Or, you can click on the launch
workspace button on the side.
0:40
Now let's go ahead and create a file that
we're going to call block_examples.rb.
0:44
And we can go ahead and
close the welcome document.
0:52
Now let's go ahead and
write a simple block.
0:57
We're just gonna do a quick loop,
and print Hello world to the screen.
1:01
And then close it by writing end.
1:08
Now we go through and everything in
between do and end is part of the block.
1:11
Let's go ahead and run it,
and just make sure it works.
1:20
Now you'll notice there's no
conditional to exit the block.
1:23
So we're going to have
to exit it using Ctrl+C.
1:26
So as we can see, it keep s
printing hello world to the screen.
1:29
And we can see after hitting Ctrl+C
that this interrupts the loop.
1:35
Now we can also write
the loop using curly braces.
1:39
And I'm gonna clear my screen here and
1:46
if we run this again we
should see the same thing.
1:48
And we do.
1:53
Now it may take a moment once you hit
Ctrl+C for it to stop and that's okay.
1:54
That's perfectly normal.
1:59
It might be your Internet
catching up with workspaces.
2:00
So, here we go.
Now, let's go ahead and
2:04
change this block a little bit so we don't
have to do Ctrl+C every single time.
2:06
We'll change this to
printing it out three times.
2:13
Now, with block syntax it doesn't
really matter if you use do and end, or
2:19
the curly braces.
2:24
There are some nuances,
with the precedents of those.
2:25
The curly braces have higher precedence.
2:30
But we don't really need to
worry about that right now.
2:33
Now when we have these blocks,
choosing between curly braces, and do and
2:36
end is usually a matter of convention.
2:41
Most Rubyists when writing a block,
if the block only
2:45
takes up a single line, like this
one does, we will use curly braces.
2:50
The other convention is,
if it takes up multiple lines,
2:55
to put that inside of a do end block.
3:00
And that's not done for
any functionality reasons.
3:07
It is done for visual separation, and
it just kind of looks a little bit better,
3:11
potentially depending on your preferences,
if multiline blocks are within do and end.
3:15
Now here's something
interesting about blocks.
3:20
You can't use the return statement inside.
3:23
So, here's an example, if we run this,
3:30
this is going to error out,
saying there's an unexpected return.
3:32
So, you can't use the return
statement inside of blocks.
3:38
If you are going to return something from
a block, it needs to use an implicit
3:42
return, which would just be the return
value of the last line of the block.
3:47
So if we run that again,
3:54
we can see it doesn't error out with
that local jump error this time.
3:55
You need to sign up for Treehouse in order to download course files.
Sign up