proc-object

A method that applies self to a proc

落花浮王杯 提交于 2020-01-16 11:48:23
问题 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

Ruby - lambda vs. Proc.new [duplicate]

孤街浪徒 提交于 2019-12-18 12:47:10
问题 This question already has answers here : Closed 8 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

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

冷暖自知 提交于 2019-12-18 04:32:09
问题 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.

What are the advantages of proc functions to methods

冷暖自知 提交于 2019-12-14 00:33:45
问题 I was solving some problems on Project Euler and I mentioned that I always wrap short methods in proc functions. I asked myself " Why? ". The answer was " I don't know. Maybe because it is short? ". So what are the advantages of proc functions to ordinary methods except that they are short :) # Proc is_prime = proc{|number| !((number%2 == 0) || (3..Math.sqrt(number).to_i).step(2).any?{|n| (number%n).zero?})} # Ordinary method def is_prime(number) !((number%2 == 0) || (3..Math.sqrt(number).to

Inconsistency of arity between Hash.each and lambdas

六眼飞鱼酱① 提交于 2019-12-12 07:20:45
问题 I lifted the following example from Josh Susser def strip_accents params thunk = lambda do |key,value| case value when String then value.remove_accents! when Hash then value.each(&thunk) end end params.each(&thunk) end when I put it in the the rails console (irb), and call it with a hash, I get the following: ruby-1.9.2-p136 :044 > `ruby --version` => "ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]\n" ruby-1.9.2-p136 :045 > strip_accents({:packs=>{:qty=>1}}) ArgumentError: wrong

What are the advantages of proc functions to methods

坚强是说给别人听的谎言 提交于 2019-12-05 21:48:40
I was solving some problems on Project Euler and I mentioned that I always wrap short methods in proc functions. I asked myself " Why? ". The answer was " I don't know. Maybe because it is short? ". So what are the advantages of proc functions to ordinary methods except that they are short :) # Proc is_prime = proc{|number| !((number%2 == 0) || (3..Math.sqrt(number).to_i).step(2).any?{|n| (number%n).zero?})} # Ordinary method def is_prime(number) !((number%2 == 0) || (3..Math.sqrt(number).to_i).step(2).any?{|n| (number%n).zero?}) end Being able to pass them around and to store them in data

Why does the break statement in ruby behave differently when using Proc.new v. the ampersand sign?

核能气质少年 提交于 2019-12-01 00:59:21
问题 The break statement for blocks (as per The Ruby Programming Language) is defined as follows: it causes the block to return to its iterator and the iterator to return to the method that invoked it. Therefore when the following code is run, it results in a LocalJumpError. def test puts "entering test method" proc = Proc.new { puts "entering proc"; break } proc.call # LocalJumpError: iterator has already returned puts "exiting test method" end test While the following code does not throw a

Ruby: convert proc to lambda?

我们两清 提交于 2019-11-30 06:32:34
问题 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! 回答1: 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

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

一笑奈何 提交于 2019-11-29 05:31:59
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 You cannot marshal a Lambda or Proc. This is because both of them are considered closures, which

How do you stringize/serialize Ruby code?

怎甘沉沦 提交于 2019-11-29 03:19:42
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