What can procs and lambdas do that functions can’t in ruby

I’ve been working in Ruby for the last couple weeks, and I’ve come to the subject of procs, lambdas and blocks. After reading a fair share of examples from a variety of sources, I don’t how they’re much different from small, specialized functions. It’s entirely possible that the examples I’ve read aren’t showing the power behind procs and lambdas.

def zero_function(x)
    x = x.to_s
   if x.length == 1
    return x = "0" + x
   else
    return x
   end
end

zero_lambda = lambda {|x|
  x = x.to_s
   if x.length == 1
    return x = "0" + x
   else
    return x
   end
}

zero_proc = Proc.new {|x|
  x = x.to_s
   if x.length == 1
    puts x = "0" + x
   else
    puts x
   end
}


puts zero_function(4)
puts zero_lambda.call(3)
zero_proc.call(2)

This function, proc, and lambda do the exact same thing, just slightly different. Is there any reason to choose one over another?

6

First of all, what do you mean by “functions”? Proc and lambda are “functions” — and they evaluate to a function object, which could be assigned to a variable or used in any other expression. There is no other way to define a non-method “function” in Ruby.

def, by contrast, defines a method in the current module (you are always inside some module, either explicitly or implicitly). A method is obviously different from a bare function because a method implicitly has access to self and the instance variables of self.

More importantly, Proc and lambda are closures — they capture local variables from the enclosing scope where they were created, and you can use those variables inside the closure. This allows function objects to hold state.

By contrast, defining a method inside another method definition will not give access to the local variables in the outer method body. So it’s the same as if you moved the inner definition outside, except that the method defined by the inner def will only exist after the method defined by the outer def is run.

Good question! I’m curious on the answers.

One difference I know:

You get no error with this call:

zero_proc.call()

But you get a wrong number of arguments (0 for 1) (ArgumentError)-error with the following calls:

puts zero_function()
puts zero_lambda.call()

Methods and lambdas check the number of parameters, Proc-objects don’t, the use nil if there are missing parameters.

The other big difference between procs, methods and lambdas is how they deal with break and return.

Basically, a return in a Proc will jump out of any enclosing method as well, and a return from a lambda will only exit the lambda.

A break from a Proc tries to break out of an enclosing iteration (if there’s no iteration, you’ll get an error), while a break from a lambda is just like a return.

More details here: http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật