惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
Engineering at Meta
Engineering at Meta
I
InfoQ
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 【当耐特】
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
腾讯CDC
雷峰网
雷峰网
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
D
Docker

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
Returning from Ruby Blocks, Procs and Lambdas
2025-09-29 · via Stonecharioteer on Tech

I was watching a youtube video about Ruby Blocks, Procs, and Lambdas and noticed something interesting.

Let’s do this with some Ruby code.

1
2
3
4
5
fruits = ["apple", "orange", "kiwi", "mango", "banana"]

fruits.each do |fruit|
  puts "I like to eat #{fruit}" unless fruit == "kiwi"
end

This is a simple block, and if you don’t feel you understand them because you’re new to Ruby (like me at the time of this writing), you should read this StackOverflow answer.

Let’s add a return to this.

1
2
3
4
5
6
fruits = %w[apple orange kiwi mango banana]

fruits.each do |fruit|
  puts "I like to eat #{fruit}" unless fruit == "kiwi"
  return "unacceptable" if fruit == "kiwi"
end

The return here works like a break in a traditional loop. It doesn’t really return anything since the each method isn’t returning anything. However, this exits out of the scope that’s using the block. Which means that this return is just exitting out of my script here.

Let’s put this inside a function to make things easier for us to snoop around.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def main
  fruits = %w[apple orange kiwi mango banana]

  fruits.each do |fruit|
    puts "I like to eat #{fruit}" unless fruit == 'kiwi'
    return fruit if fruit == 'kiwi'
  end
end

fave_fruit = main
puts "I love #{fave_fruit}!"

Here, the method exits after 2 runs, and I get the value of fave_fruit.

The return exits us out of the scope that it’s called it. Quite neat. But I’m certain this is going to trip me up some day so I’m writing it down.

If I were to do this with a Proc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def main
  fruits = %w[apple orange kiwi mango banana]

  fruit_proc = Proc.new do |fruit|
    puts "I like to eat #{fruit}" unless fruit == 'kiwi'
    return fruit if fruit == 'kiwi'
  end

  fruits.each(&fruit_proc)
  puts "I've processed all the fruits."
end

fave_fruit = main
puts "I love #{fave_fruit}!"
$ ruby code/posts/ruby-block-return/ex_1.rb
I like to eat apple
I like to eat orange
I love kiwi!

This block does the same thing, with procs. Notice how the puts "I've processed all the fruits." line is not printing.

Let’s try this with a lambda:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def main
  fruits = %w[apple orange kiwi mango banana]
  fruit_lambda = lambda do |fruit|
    puts "I like to eat #{fruit}" unless fruit == 'kiwi'
    fruit if fruit == 'kiwi'
  end
  fruits.each(&fruit_lambda)
  puts "I've processed all the fruits."
end
fave_fruit = main
puts "I love #{fave_fruit}!" unless fave_fruit.nil?

The output is very different now.

❯ ruby code/posts/ruby-block-return/ex_1.rb
I like to eat apple
I like to eat orange
I like to eat mango
I like to eat banana
I've processed all the fruits.

It doesn’t get the return value fruit if fruit == 'kiwi' at all now.

A lambda is useful when you want to eject only out of the current loop call, not the entire scope that you’re calling this from.

I don’t really have many more observations, but if I do, I’ll write another post.