天道酬勤,学无止境

proc-object

Ruby - lambda vs. Proc.new [duplicate]

This question already has answers here: Closed 9 years ago. Possible Duplicate: What's the difference between a proc and a lambda in Ruby? When run this Ruby code: def func_one proc_new = Proc.new {return "123"} proc_new.call return "456" end def func_two lambda_new = lambda {return "123"} lambda_new.call return "456" end puts "The result of running func_one is " + func_one puts "" puts "The result of running func_two is " + func_two The result that I get is as follows: The result of running func_one is 123 The result of running func_two is 456 As for func_two, where is the the value of the

2021-04-24 02:39:11    分类:问答    ruby   lambda   return   proc-object

Ruby Proc语法(Ruby Proc Syntax)

问题 我昨天在这里提出的一个问题的答案是以下一段Ruby代码: def overlap?(r1,r2) r1.include?(r2.begin) || r2.include?(r1.begin) end def any_overlap?(ranges) ranges.sort_by(&:begin).each_cons(2).any? do |r1,r2| overlap?(r1, r2) end end 我得到了each_cons ,但是&:begin表示法是什么呢? 从语法地狱救我! 谢谢! 回答1 当在呼叫的最后一个参数前加上&前缀时&您会清楚地表明您是在发送一个块,而不是普通的参数。 好的,在method(&:something) , :something是一个符号,而不是proc ,因此Ruby会自动调用to_proc方法以获取实际块。 Rails伙计们(现在也包括香草Ruby)巧妙地将其定义为: class Symbol def to_proc proc { |obj, *args| obj.send(self, *args) } end end 这就是为什么您可以这样做: >> [1, 2, 3].map(&:to_s) # instead of [1, 2, 3].map { |n| n.to_s } => ["1", "2", "3"] [edit]注意

2021-04-19 01:32:17    分类:技术分享    ruby   syntax   proc-object

How do I marshal a lambda (Proc) in Ruby?

Joe Van Dyk asked the Ruby mailing list: Hi, In Ruby, I guess you can't marshal a lambda/proc object, right? Is that possible in lisp or other languages? What I was trying to do: l = lamda { ... } Bj.submit "/path/to/ruby/program", :stdin => Marshal.dump(l) So, I'm sending BackgroundJob a lambda object, which contains the context/code for what to do. But, guess that wasn't possible. I ended up marshaling a normal ruby object that contained instructions for what to do after the program ran. Joe

2021-04-16 15:02:52    分类:问答    ruby   serialization   lambda   proc-object

How do you stringize/serialize Ruby code?

I want to be able to write a lambda/Proc in my Ruby code, serialize it so that I can write it to disk, and then execute the lambda later. Sort of like... x = 40 f = lambda { |y| x + y } save_for_later(f) Later, in a separate run of the Ruby interpreter, I want to be able to say... f = load_from_before z = f.call(2) z.should == 42 Marshal.dump does not work for Procs. I know Perl has Data::Dump::Streamer, and in Lisp this is trivial. But is there a way to do it in Ruby? In other words, what would be the implementation of save_for_later? Edit: My answer below is nice, but it does not close over

2021-04-16 01:49:38    分类:问答    ruby   serialization   lambda   proc-object

Ruby: convert proc to lambda?

Is it possible to convert a proc-flavored Proc into a lambda-flavored Proc? Bit surprised that this doesn't work, at least in 1.9.2: my_proc = proc {|x| x} my_lambda = lambda &p my_lambda.lambda? # => false!

2021-04-14 06:01:15    分类:问答    ruby   lambda   proc-object

Ruby: Proc.new { 'waffles' } vs. proc { 'waffles' }

In Ruby, are there any differences between Proc.new { 'waffles' } and proc { 'waffles' }? I have found very few mentions of the second syntax. From testing using irb, I haven't found any obvious differences. Is the second syntactic sugar for the first?

2021-04-14 04:02:28    分类:问答    ruby   syntax   proc-object

在Ruby块中使用“返回”(Using 'return' in a Ruby block)

问题 我正在尝试将Ruby 1.9.1用于嵌入式脚本语言,以便将“最终用户”代码写入Ruby块中。 与此相关的一个问题是,我希望用户能够在块中使用'return'关键字,因此他们不必担心隐式的返回值。 考虑到这一点,这是我想做的事情: def thing(*args, &block) value = block.call puts "value=#{value}" end thing { return 6 * 7 } 如果在上面的示例中使用“ return”,则会收到LocalJumpError。 我知道这是因为所讨论的块是Proc而不是lambda。 如果删除“ return”,该代码将起作用,但在这种情况下,我真的更希望能够使用“ return”。 这可能吗? 我尝试将块转换为lambda,但结果是相同的。 回答1 在这种情况next ,只需使用next : $ irb irb(main):001:0> def thing(*args, &block) irb(main):002:1> value = block.call irb(main):003:1> puts "value=#{value}" irb(main):004:1> end => nil irb(main):005:0> irb(main):006:0* thing { irb(main):007:1*

2021-04-10 02:52:43    分类:技术分享    ruby   lambda   return   proc-object

Why does explicit return make a difference in a Proc?

def foo f = Proc.new { return "return from foo from inside proc" } f.call # control leaves foo here return "return from foo" end def bar b = Proc.new { "return from bar from inside proc" } b.call # control leaves bar here return "return from bar" end puts foo # prints "return from foo from inside proc" puts bar # prints "return from bar" I thought the return keyword was optional in Ruby and that you are always returning whether you request it or not. Given that, I find it surprising that foo and bar have different output determined by the fact that foo contains an explicit return in Proc f

2021-04-03 23:13:48    分类:问答    ruby   return   proc-object

Ruby Proc Syntax

An answer to a question I posed yesterday on here was the following piece of Ruby code: def overlap?(r1,r2) r1.include?(r2.begin) || r2.include?(r1.begin) end def any_overlap?(ranges) ranges.sort_by(&:begin).each_cons(2).any? do |r1,r2| overlap?(r1, r2) end end I get each_cons, but what's the strange &:begin notation? Save me from syntax hell! Thanks!

2021-03-28 05:55:07    分类:问答    ruby   syntax   proc-object

Using 'return' in a Ruby block

I'm trying to use Ruby 1.9.1 for an embedded scripting language, so that "end-user" code gets written in a Ruby block. One issue with this is that I'd like the users to be able to use the 'return' keyword in the blocks, so they don't need to worry about implicit return values. With this in mind, this is the kind of thing I'd like to be able to do: def thing(*args, &block) value = block.call puts "value=#{value}" end thing { return 6 * 7 } If I use 'return' in the above example, I get a LocalJumpError. I'm aware that this is because the block in question is a Proc and not a lambda. The code

2021-03-27 12:12:02    分类:问答    ruby   lambda   return   proc-object