天道酬勤,学无止境

proc-object

A method that applies self to a proc

Question I want to have a method defined on Object that takes a block and applies the receiver to the block. An implementation will be like the following: class Object def apply ≺ pr.call(self) end end 2.apply{|x| x * 3} # => 6 Is there already a standard way to do this or a well known library that has a method with similar use? If so, I didn't want to reinvent the wheel. It happens to me very often that, I have a method that takes an optional block, and when there is no block, I want to return some return_value calculated within the method, but when there is a block, I want to return the

2021-10-31 19:46:56    分类:技术分享    ruby   ruby-1.9.3   proc-object

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

Question 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? Answer1 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.

2021-10-19 23:36:02    分类:技术分享    ruby   syntax   proc-object

Ruby: convert proc to lambda?

Question 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! Answer1 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 procs and lamdbas. 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

2021-05-08 00:32:30    分类:技术分享    ruby   lambda   proc-object

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

Question 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 Answer1 You cannot marshal a Lambda or Proc. This is because both of them are considered

2021-05-06 13:32:25    分类:技术分享    ruby   serialization   lambda   proc-object

How do you stringize/serialize Ruby code?

Question 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

2021-04-29 07:29:41    分类:技术分享    ruby   serialization   lambda   proc-object

Why does explicit return make a difference in a Proc?

Question 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

2021-04-24 15:07:26    分类:技术分享    ruby   return   proc-object

Ruby Proc Syntax

Question 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! Answer1 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

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

Using 'return' in a Ruby block

Question 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

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