Skip to content

Instantly share code, notes, and snippets.

@kvirani
Last active April 25, 2025 17:56
Show Gist options
  • Select an option

  • Save kvirani/f7e8918546eb66559d78 to your computer and use it in GitHub Desktop.

Select an option

Save kvirani/f7e8918546eb66559d78 to your computer and use it in GitHub Desktop.
Blocks in Ruby
def benchmark
# Your benchmarking code goes here.
end
# Be careful, pasting this into IRB will take a long time to print.
# It's a loooong string. :)
long_string = "apple"*100000000
running_time = benchmark { long_string.reverse }
puts "string.reverse took #{running_time} seconds to run"

This is a small challenge to learn more about how blocks work in Ruby. Blocks are actually very similiar to methods.

So let's think about methods for a second... You call a method with data from the outside world — the method's arguments. The code inside the method can see and use this data.

If arguments are how we pass in data into methods, blocks are how we pass in behavior. Think of them as a chunk of logic or a "brain" that your method can run (aka: "call" or "yield").

In Ruby, blocks can be passed into methods as a sort of "invisible argument", like this:

def print_result
  block_execution = yield
  puts block_execution
end

# This will print out the number 9 to the console
print_result { 3 * 3 }

# Blocks can also be written using the do...end format
print_result do
  creature = "walrus"
  "I am the #{creature}!"
end

# Blocks have access to variables outside of their definition

shopping_list = [:milk, :eggs, :cheese]
print_result do
  important_item = shopping_list.sample
  "I hope I don't forget #{important_item}!!!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment