The yield Keyword in Ruby

Mehdi Farsi
Apr 11, 2018 · 3 min read

What’s a block ?

one yield
multiple yields
multiple yields

block_given?

Arguments

Return value

Array#my_map

$> array = [1, 2, 3]
=> [1, 2, 3]
$> array.map {|n| n + 2}
=> [3, 4, 5]
$> array
=> [1, 2, 3]
$> array = [1, 2, 3]
=> [1, 2, 3]
$> array.my_map {|n| n + 2}
=> [3, 4, 5]
$> array
=> [1, 2, 3]
$> array.my_map
LocalJumpError (no block given (yield))
$> array.my_map
=> [1, 2, 3]

ONE MORE THING ⬇

Feel free to subscribe here: www.rubycademy.com


rubycademy

E-Learning platform for Ruby and Ruby on Rails

Mehdi Farsi

Written by

Founder of www.rubycademy.com — I blog about Ruby & ROR - Paris

rubycademy

E-Learning platform for Ruby and Ruby on Rails