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