Blocs, Procs, and Lambdas
Ruby Blocks, Procs, Lambdas
Blocks
A ruby block is the piece of code that is executed. This code can be executed using curly brackets {} or a pair of "do" and "end". Blocks can be used in conjunction with #each, #times, #collect, and other built-in methods. For example, in figure below are two ways in which we can print out the strings in the array.
The yield statement invokes a block. A yield statement can also be used with parameters for invoking a block. Let's take a look at this next example.
When yield is executed it runs the test statement "now in the block". Basically when yield is in the block it will run the test method with the block "{puts 'now in the block'}".
This is how a yield statement would look like with parameters.
By interpolating the names (inserting the argument "name") into the string we can call yield with an argument which will be used in the show_name method and the corresponding block.
Procs
In Ruby blocks cannot be saved to variables to write D.R.Y. code because blocks aren't objects. Procs come to the rescue here because they are objects themselves. Procs are "saved" blocks. They are simple to define. In the example below we create a new Proc for "multiply" and use multiply for both an_array and another_array without having to rewrite the math block. We just call "&"multiply". Collect! and Map! do the same thing; they both take each element from the array and replace the original element with the new element. Also procs can be called on via the ".call" method. If we had a new proc with the code block of "puts "hello!", we could use the name of that proc and add ".call" and the proc would run. We can also use the "&" to convert symbols as procs. In the example below, we turn the elements in string_array into integers by using the ":to_i" symbol.
Lambdas
Lambdas are very similar to procs. Lambda syntax is different from procs though. Previously we saw that a new proc would need "variable_name = Proc.new {code block}". Lambdas replace "Proc.new" with "lambda". Lambdas also use the ampersand to call the "stored" block. The below is an example of lambda syntax.
There are two main differences between procs and lambdas. The first differences is that lambda checks the number of arguments passed to it, whereas procs took whatever was passed to it. However procs would output "nil" for the extra arguments. Lambdas will throw errors if unexpected arguments were passed to it. Second, when a lambda returns it passes control back to the calling method but a proc immediately returns WITHOUT going back to the calling method. Need a visual explanation? Check out the example below.
The proc example immediately returns "Celica will win!" without going back to celica_gtr_proc method. On the other hand, the lambda example goes back to the celica_gtr_lambda method and outputs "GTR will win!".
Posts
-
The Post DBC Experience
Mixed Emotions
DBC was an awesome, maybe one of a kind experience. I pushed through all 4 phases with the mantids, or the improper plural...
-
How to Ask for Help
How to Ask Good Questions
Experienced Questioning
I don't always ask questions online but when I do I try to be as specific as possible...
-
Ruby on Rails
Rails
Ruby and Rails are not the same thing. Ruby is the programming language. Rails is a Rubygem or package library which is installed via...
-
Resolving Conflicts
A Conflict I Experienced
Honestly, I cannot remember a conflict I've experienced off the top of my head that would be exciting to read or...
- My Thinking Ways How do I Think? I am a concrete sequential thinker which according to cgribben.com mean that I am persistent and product oriented. I am practical...
-
SQL Injection
What is SQL?
SQL is short for Structured Query Language. It is a programming language for querying (fancy word for accessing) and managing databases. SQL...
-
Affirmation and Stereotype Threat
My Values
Accomplishment, accountability, compassion, creativity, efficiency, family, friendships, and integrity area few values which come to mind when I recall the times in my...
-
Blocs, Procs, and Lambdas
Ruby Blocks, Procs, Lambdas
Blocks
A ruby block is the piece of code that is executed. This code can be executed using curly brackets {}...
-
Stereotype Threat
What is Stereotype Threat?
To put it simply, stereotype threat is the experience of not living or carrying out your full potential because of stereotypes....
-
A Pair of Minds
Pairing and Feedback
Pairing
Pairing isn't so bad. The peers are all nice and I haven't met someone who was disrespectful during the sessions. Despite...
-
A Little Ruby
A Snippet into Ruby Classes
When and Why Should I use classes?
One question which kept simmering in my head while working on this week's...
-
Ruby Cycle and Looping
Ruby:Enumberable#Cycle
The #cycle method uses a block for each element of an enumerable object (ranges, hash, or array) repeatedly for n times or endlessly if...
-
Today's Software Apps vs Law
Who Doesn’t Like App Efficiency?
Innovative technologies usually clash with old ways and methods. There are numerous apps out there which are constantly clashing with...
-
What is JS?
What is JavaScript and Why Should You Care?
Javascript(JS) is an object oriented computer programming language often found as a part of web browsers which...
-
CSS Positioning
Positioning in CSS
Elements on a website can be positioned behind, in front of, or next to by using CSS positioning properties: top, left, right,...
-
My Favorite Websites
My 3 Most Visited Websites
Newcelica.org
This is one of my favorite sites because I own a Celica. I do all...
-
Kitchen Vs Table
Kitchen Vs Table
DBC sounds like a buttkicker, a mental, emotional, and physical obstacle course. Honestly, it sounds like it would and should be overwhelming...