yield
? (Ruby) Show an example. - [1]
yield
calls the block which was passed into a method. The parameters to yield
will be passed to the block./^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$
undefined
, a variable that exists with no value is null
yield
def yield_param_to_block(param)
yield param
end
yield_param_to_block("Hello, world!") do |yieled_param|
puts yieled_param
end
def append_in_ruby_to(sentence)
"#{sentence} in Ruby"
end
def convert(str)
str.match /^\$([0-9]+)\.([0-9]{1,2})$/ do |m|
dollars = m[1].to_i
cents = m[2].to_i
out = {}
# All dollars become quarters
out[:quarters] = dollars * 4
# Use integer division to find out how to make change
out[:quarters] += cents / 25
# Modulo to see how many cents are left after taking out the quarters
cents = cents % 25
# Do dimes
out[:dimes] = cents / 10
cents = cents % 10
# And nickels (of which there can/may only be one)
out[:nickels] = cents / 5
# Anything left is pennies
out[:pennies] = cents % 5
# Don't forget to return
out
end
end