Questions tagged [proc-object]
47 questions
                    
                    102
                    
            votes
                
                8 answers
            
        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…
         
    
    
        MetaFu
        
- 1,021
- 2
- 8
- 3
                    59
                    
            votes
                
                3 answers
            
        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…
         
    
    
        uzo
        
- 2,821
- 2
- 25
- 26
                    45
                    
            votes
                
                3 answers
            
        Ruby - lambda vs. Proc.new
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…
         
    
    
        Simplicity
        
- 47,404
- 98
- 256
- 385
                    34
                    
            votes
                
                1 answer
            
        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…
         
    
    
        ClosureCowboy
        
- 20,825
- 13
- 57
- 71
                    21
                    
            votes
                
                2 answers
            
        Why use procs instead of methods?
I'm new to programming, and ruby is my first real run at it.  I get blocks, but procs seem like a light method/function concept -- why use them?  Why not just use a method?
Thanks for your help.
         
    
    
        Nathan
        
- 7,627
- 11
- 46
- 80
                    21
                    
            votes
                
                7 answers
            
        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 =>…
         
    
    
        James A. Rosen
        
- 64,193
- 61
- 179
- 261
                    17
                    
            votes
                
                4 answers
            
        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…
         
    
    
        Jonathan Tran
        
- 15,214
- 9
- 60
- 67
                    16
                    
            votes
                
                5 answers
            
        Ruby block, procs and instance_eval
I recently tried to do something akin to this:
a = "some string"
b = Proc.new{ upcase }
a.instance_eval b
Which gives the error:
TypeError: can't convert Proc into String
but this works:
def b(&block)
  "some string".instance_eval &block
end
b{…
         
    
    
        brad
        
- 31,987
- 28
- 102
- 155
                    16
                    
            votes
                
                5 answers
            
        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!
         
    
    
        Alan O'Donnell
        
- 1,276
- 9
- 17
                    13
                    
            votes
                
                2 answers
            
        Why use Proc.new to call a method in a Rails callback?
in all the tutorials for RoR I see instances where the coder chose to use Proc.new when seemingly it is both unnecessary and rather unattractive.
Example, here is a callback for placed in a model, one using Proc.new the other presumably doing the…
         
    
    
        MusikAnimal
        
- 2,286
- 2
- 22
- 28
                    13
                    
            votes
                
                1 answer
            
        Is this a bug in Method#to_proc? (Ruby 1.8.7)
Given the following method that takes one argument:
def foo(arg); p arg; end
I can call it with an empty array:
foo([])
# prints []
I can also save it as a Method object and call that with an empty array, with the same…
         
    
    
        Sam Stokes
        
- 14,617
- 7
- 36
- 33
                    10
                    
            votes
                
                3 answers
            
        How to use a variable as default value of a TCL proc argument
I've got a variable that I would like to use as default value for an argument:
proc log {message {output $::output}} {
   ....
}
Is there a way to do this or need I to evaluate the variable inside my proc?
         
    
    
        patrick
        
- 1,282
- 4
- 21
- 37
                    10
                    
            votes
                
                4 answers
            
        TCL - how to know how much time a function has worked?
Say I have a proc and the proc consists of several statements and function calls. How I can know how much time the function has taken so far?
         
    
    
        Narek
        
- 38,779
- 79
- 233
- 389
                    9
                    
            votes
                
                3 answers
            
        How to pass parameters to a proc when calling it by a method?
proc = Proc.new do |name|
  puts "Thank you #{name}!"
end
def thank
  yield
end
proc.call # output nothing, just fine
proc.call('God') # => Thank you God!
thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank…
         
    
    
        Croplio
        
- 3,433
- 6
- 31
- 37
                    8
                    
            votes
                
                3 answers
            
        Why does the break statement in ruby behave differently when using Proc.new v. the ampersand sign?
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…
         
    
    
        Salman Paracha
        
- 1,697
- 2
- 16
- 27