proc-object

Ruby: convert proc to lambda?

 ̄綄美尐妖づ 提交于 2019-11-28 19:39:58
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! This one was a bit tricky to track down. Looking at the docs for Proc#lambda? for 1.9 , there's a fairly lengthy discussion about the difference between proc s and lamdba s. What it comes down to is that a lambda enforces the correct number of arguments, and a proc doesn't. And from that documentation, about the only way to convert a proc into a lambda is shown in this example: define

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

一笑奈何 提交于 2019-11-28 19:10:56
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? From Metaprogamming Ruby Page 113. In Ruby 1.8, Kernel#proc() is actually a synonym for Kernel#lambda(). Because of loud protest from programmers, Ruby 1.9 made proc() a synonym for Proc.new() instead. 来源: https://stackoverflow.com/questions/4710538/ruby-proc-new-waffles-vs-proc-waffles

How do you stringize/serialize Ruby code?

三世轮回 提交于 2019-11-27 17:32:22
问题 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

Why does explicit return make a difference in a Proc?

回眸只為那壹抹淺笑 提交于 2019-11-27 10:39:30
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 return ing 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 .

Using 'return' in a Ruby block

亡梦爱人 提交于 2019-11-27 00:16:16
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

Ruby Proc Syntax

倾然丶 夕夏残阳落幕 提交于 2019-11-26 23:02:43
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! When you prefix the last argument of a call with & you are making clear that you are sending a block and not a normal argument. Ok, in method(&:something) , :something is a symbol, not a proc , so Ruby automatically calls the method to_proc to

Why does explicit return make a difference in a Proc?

做~自己de王妃 提交于 2019-11-26 15:16:47
问题 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 return ing whether you request it or not. Given that, I find it surprising that foo

Using 'return' in a Ruby block

▼魔方 西西 提交于 2019-11-26 12:21:43
问题 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

Ruby Proc Syntax

╄→尐↘猪︶ㄣ 提交于 2019-11-26 08:34:40
问题 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! 回答1: When you prefix the last argument of a call with & you are making clear that you are sending a block and not a normal argument. Ok, in