Ruby Blocks and yield 2020
A Ruby Code block (aka closure) s a way of grouping statements so that we can associate with method invocations as if they were parameters.
The Ruby standard is to use braces for single-line blocks and do-end for multi-line blocks.
Once we have created a block, we can associate it with a call to a method. Usually the code blocks passed into methods are anonymous objects, created on the spot. For example, in the following code, the block containing puts "Hello" is associated with the call to a method hello.
hello {puts 'Hello'}
If the method has parameters, they appear before the block:
hello("RubyOnRails") {puts 'Hello'}
A method can then invoke an associated block using the Ruby yield statement. So, any method that wants to take a block as a parameter can use the yield keyword to execute the block at any time.
If we provide a code block when we call a method, then inside the method, we can yield control to that code block - suspend execution of the method; execute the code in the block; and return control to the method body, right after the call to yield:
#!/usr/bin/env ruby # block1.rb def rb_block puts 'Start' # you can call the block using the yield keyword yield yield puts 'End' end # Code blocks may appear only in the source adjacent to a method call rb_block {puts "We're in the block"}
Output:
Start We're in the block We're in the block End
We can provide parameters to the call to yield: these will be passed to the block. Within the block, we list the names of the arguments to receive the parameters between vertical bars (|).
#!/usr/bin/env ruby # block2.rb def rb_block yield('hello', 2014) end # Code blocks may appear only in the source adjacent to a method call rb_block { |s, num| puts s + ' ' + num.to_s}
Output:
hello 2014
As we can see, the code in the block is not executed at the time it is encountered by the Ruby interpreter. Instead, Ruby remembers the context in which the block appears and then enters the method.
Ruby on Rails
- Ruby On Rails Home
- Ruby - Input/Output, Objects, Load
- Ruby - Condition (if), Operators (comparison/logical) & case statement
- Ruby - loop, while, until, for, each, (..)
- Ruby - Functions
- Ruby - Exceptions (raise/rescue)
- Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)
- Ruby - Class and Instance Variables
- Ruby - Class and Instance Variables II
- Ruby - Modules
- Ruby - Iterator : each
- Ruby - Symbols (:)
- Ruby - Hashes (aka associative arrays, maps, or dictionaries)
- Ruby - Arrays
- Ruby - Enumerables
- Ruby - Filess
- Ruby - code blocks and yield
- Rails - Embedded Ruby (ERb) and Rails html
- Rails - Partial template
- Rails - HTML Helpers (link_to, imag_tag, and form_for)
- Layouts and Rendering I - yield, content_for, content_for?
- Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag
- Rails Project
- Rails - Hello World
- Rails - MVC and ActionController
- Rails - Parameters (hash, array, JSON, routing, and strong parameter)
- Filters and controller actions - before_action, skip_before_action
- The simplest app - Rails default page on a Shared Host
- Redmine Install on a Shared Host
- Git and BitBucket
- Deploying Rails 4 to Heroku
- Scaffold: A quickest way of building a blog with posts and comments
- Databases and migration
- Active Record
- Microblog 1
- Microblog 2
- Microblog 3 (Users resource)
- Microblog 4 (Microposts resource I)
- Microblog 5 (Microposts resource II)
- Simple_app I - rails html pages
- Simple_app II - TDD (Home/Help page)
- Simple_app III - TDD (About page)
- Simple_app IV - TDD (Dynamic Pages)
- Simple_app V - TDD (Dynamic Pages - Embedded Ruby)
- Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)
- App : Facebook and Twitter Authentication using Omniauth oauth2
- Authentication and sending confirmation email using Devise
- Adding custom fields to Devise User model and Customization
- Devise Customization 2. views/users
- Rails Heroku Deploy - Authentication and sending confirmation email using Devise
- Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I
- Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II
- OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization