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
Ruby makes heavy use of blocks in the built in types and the standard library. In this video, we explore Strings and Integers.
Documentation Links
Code Samples
String#each_char:
string = "Treehouse"
string.each_char{|c| print "#{c}-"}
This prints out:
"T-r-e-e-h-o-u-s-e"
String#each_line:
haiku = <<-EOF
A string with three lines
Is considered multi line
Ruby code haiku
EOF
haiku.each_line{ |line| puts "* #{line}" }
Prints the following:
* A string with three lines
* Is considered multi line
* Ruby code haiku
Integer#down_to:
5.downto(3) { |number| print number }
Prints out the following:
543
[SOUND] Ruby makes heavy use of blocks in
0:00
the built in types and
the standard library.
0:04
We've encountered some block methods
before but now that we know more about
0:10
blocks let's take a fresh look at some
of the built in types using workspaces.
0:14
So now that we know more about blocks,
let's go ahead and
0:20
take a look at some of the methods that
take blocks from classes that we've
0:23
seen in the Ruby standard library.
0:28
First, let's go ahead and
take a look at Strings.
0:33
Here is the documentation for
the String class.
0:36
And if we scroll down here on the left,
let's go ahead and
0:40
look at this each_char method.
0:43
Now when we look at the documentation for
something that takes a block, or
0:46
any method really, we can see that it will
show you what arguments the method takes.
0:50
Now when we look at this we can see that
this takes a block with one argument.
0:56
The block is here and
1:02
this arrow means that it returns a String.
1:05
So let's go ahead and open up our
workspace, and I've already launched one.
1:10
I've got it inside of IRB.
1:15
So let's go ahead and take a String
here and we'll just say it's a string.
1:18
And we'll set it to Treehouse, and
1:21
let's go ahead and
practice that each_char method.
1:25
Let's say sting.each_char, and
then we send in the block.
1:27
We know it takes one argument,
we're just gonna call it c, and
1:32
let's print that out,
with a dash right after it.
1:37
So now we can see that this prints out
Treehouse, each character of the String
1:45
and it returns a String itself which
happens to be the original String.
1:51
Now that's useful because we
can do something after that.
1:58
We can kinda chain these
methods if we want to.
2:02
And this doesn't really have anything to
do with the block, it's just kinda cool.
2:04
We could call upcase on that and
then we can see that the actual return is
2:08
TREEHOUSE which is the rightmost
method that's called.
2:12
Now let's go ahead and
look at another method here.
2:17
In addition to each_char,
let's look at each_line.
2:20
This is another method
that takes a block and
2:24
lines are separated by
a new line character.
2:27
So let's go ahead and
test that out in IRB.
2:31
So let's go ahead and
create multi-line String here.
2:33
I'm just gonna call it haiku.
2:36
And we're gonna use the here doc
format to create the String.
2:39
So, here we have our multiline String.
2:55
And then just to demonstrate the each_line
method, we can call each_line.
2:58
And we remember from the documentation,
it takes a block with one argument.
3:04
And let's go ahead and just print that
out with an asterisk in front of it.
3:09
And now we can see our multi-line String
was printed out with an asterisk in front
3:16
of it, just like we expect.
3:20
And then just like the each_char method,
a String is returned.
3:22
So, I'm gonna clear my screen here.
3:29
And then the next method that we're
gonna take a look at is on Integer.
3:30
And this is a neat one.
3:37
We can take any number and
use this downto method, well, any integer.
3:38
And this takes one argument
to the method itself.
3:44
And it takes a block
also with one argument.
3:50
They have an example here in
the documentation, which is 5.downto(1).
3:55
Here's a number, print the number,
and then two dots.
3:59
And that will produce this output.
4:04
Let's go ahead and see if we can
recreate something like that.
4:06
We can say 5.downto(3) and
we'll call that number.
4:10
And let's go ahead and
just print the number.
4:17
That should print out the numbers five,
four, and three right after each other.
4:21
There we go.
And
4:26
then we'll notice here this also returns
the original integer that we send in.
4:27
Now numbers are different, we can't
do something like (5.1).downto(3),
4:33
because the floating point numbers
don't have the downto method.
4:41
And then something else to note
when you're working with blocks,
4:48
it's not necessary to put
all these spaces here,
4:51
that's just the style that I'm
using to make it easier to see.
4:54
You need to sign up for Treehouse in order to download course files.
Sign up