This week I have started learning Ruby language. I came to know that those who have are from Python background find it easy to learn as compared to those from other languages. This motivates me and pushes me to explore Ruby.
So far I have been going through Ruby Documentation. This post is specifically dedicated towards Ruby code blocks. This is a nice feature that I see unique as compared to other languages.
Ruby code blocks gives us ability to pass a block of code to be passed as a argument to a method. :)
Looks Strange.. isn't it ? I also reacted the same way !
Let's look how it works:
Consider a method:
And let's say we have single line code block as {puts "in the block"}. We can pass this single line block as a parameter to the method the same way we do it for method parameters. In order to consume the Ruby code block the method can use the "yield" method to invoke it. The beauty is that a Ruby code block can be invoked in the same way as methods are invoked.
Let's have a look on how we go about it:
What happens if we pass no code block to a method that have a yield statement. Yes, you guessed it right ! It gives an error: LocalJumpError. Here is the small snippet showing how:
How to handle "no block given" error: We can use "block_given?" method to check whether a block is passed as param and then only invoke "yield" method. Here is the small snippet showing how:
Hope you enjoyed my first post on Ruby. Stay tuned to get more magic with Ruby !
Cheers!
So far I have been going through Ruby Documentation. This post is specifically dedicated towards Ruby code blocks. This is a nice feature that I see unique as compared to other languages.
Ruby code blocks gives us ability to pass a block of code to be passed as a argument to a method. :)
Looks Strange.. isn't it ? I also reacted the same way !
Let's look how it works:
Consider a method:
Let's have a look on how we go about it:
In the above shown code I have added "puts" and "print" for tracking. In line #5 you can observe that we are calling "yield" with no parameters at all. This will simply call the Ruby code block that it received as parameter. In line #6 you can observe that we are calling "yield" with 10 as parameter. In this case greet method will invoke Ruby code block with 10 as one of the parameter.
Single line code blocks
Now when we pass single line Ruby code block as parameter to greet as in line #11 having simple "puts" statement we get such output:
You can observe two "in the block" statement in output. The reason behind is that we invoked yield twice within greet method. Once without parameter and once with 10 as parameter.
Multi line code blocks
Now consider when we pass multiline Ruby code block as parameter to greet as in line #13 having print statement with block parameter "x" picked dynamically from yield statement in greet method. In line #14 I have used interpolation feature of Ruby that puts the result of an expression within string literal. So this will produce such output:
You can observe the two "in the block with x=" statements with different x value. The reason behind is that we invoked yield twice within greet method. Once without parameter and once with 10 as parameter. And we used interpolation to print the value of parameter.
Capturing the response from Ruby code blocks
We can capture the response from code block in the same way we do for Ruby methods.
Lets dive for one example:
I have defined a Ruby method that captures response from yield( execution of ruby code block) into a variable named "result". We have a multiline code block at line #32. As we know Ruby methods return value in last line as return value by-default. The same funda applies to Ruby code blocks. So the value that gets returned by this code block is "2" since we mentioned it in line #34 (last line of code block). And here's the output:
No block passed within a method having yield statement
What happens if we pass no code block to a method that have a yield statement. Yes, you guessed it right ! It gives an error: LocalJumpError. Here is the small snippet showing how:How to handle "no block given" error: We can use "block_given?" method to check whether a block is passed as param and then only invoke "yield" method. Here is the small snippet showing how:
Hope you enjoyed my first post on Ruby. Stay tuned to get more magic with Ruby !
Cheers!