Questions tagged [proc]

In Ruby, Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.

In Ruby, a Proc (short for procedure) is a block of code, bound to a variable. As is always the way in Ruby, a Proc is an object, and so can be created with the new method, it is generally preferable to use the lambda method.

1023 questions
345
votes
14 answers

When to use lambda, when to use Proc.new?

In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and Proc.new on the other. What are those differences? Can you give guidelines on how to decide which one to choose? In Ruby 1.9, proc and lambda are different. What's…
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
102
votes
2 answers

Why do we need fibers

For Fibers we have got classic example: generating of Fibonacci numbers fib = Fiber.new do x, y = 0, 1 loop do Fiber.yield y x,y = y,x+y end end Why do we need Fibers here? I can rewrite this with just the same Proc (closure,…
fl00r
  • 82,987
  • 33
  • 217
  • 237
63
votes
4 answers

/proc kcore file is huge

After experiencing a DDOS attack, somehow /proc/kcore is very huge, I use a small php class to check the current disk space, and how many has been used. It shows the following: Total Disk Space: 39.2 GB Used Disk Space: 98 GB Free Disk Space: 811.6…
Love2Code
  • 898
  • 1
  • 7
  • 13
63
votes
4 answers

What does to_proc method mean in Ruby?

I am learning rails and following this thread. I am stuck with the to_proc method. I consider symbols only as alternatives to strings (they are like strings but cheaper in terms of memory). If there is anything else I am missing for symbols, then…
swapnesh
  • 26,318
  • 22
  • 94
  • 126
27
votes
1 answer

How to store ruby code blocks

I want to store a "code block" in a variable to be reused, something like: block = do |test| puts test end 3.upto(8) block Can someone show me what am I doing so obviously wrong? (Or if it's just impossible)
user1241335
25
votes
4 answers

Purpose of & (ampersand) in Ruby for procs and calling methods

I've noticed that a lot of examples dealing with Ruby Procs have the following & symbol in it. # Ruby Example shout = Proc.new { puts 'Yolo!' } def shout_n_times(n, &callback) n.times do callback.call end end shout_n_times(3, &shout) #…
wmock
  • 5,382
  • 4
  • 40
  • 62
17
votes
3 answers

TCL obtain the proc name in which I am

How to know what is the name of the proc in which I am. I mean I need this: proc nameOfTheProc {} { #a lot of code here puts "ERROR: You are using 'nameOfTheProc' proc wrongly" } so I want to obtain "nameOfTheProc" but not hard-code. So…
Narek
  • 38,779
  • 79
  • 233
  • 389
15
votes
3 answers

How to replace all nil value with "" in a ruby hash recursively?

str = "" hash = Hash.from_xml(str) # => {"a"=>{"b"=>{"c"=>nil}}} How can I replace all nils in a Hash to "" so that the hash becomes: {"a"=>{"b"=>{"c"=>""}}}
ohho
  • 50,879
  • 75
  • 256
  • 383
15
votes
2 answers

How is `Proc#==` evaluated?

How is Proc#== evaluated? RDoc says: prc == other_proc → true or false Returns true if prc is the same object as other_proc, or if they are both procs with the same body. But it is not clear what counts as having "the same body". One condition…
sawa
  • 165,429
  • 45
  • 277
  • 381
14
votes
5 answers

Is it possible to see the ruby code in a proc?

p = Proc.new{ puts 'ok' } Is is possible to see the ruby code in the proc? inspect returns the memory location: puts p.inspect # Ruby 1.9.3
B Seven
  • 44,484
  • 66
  • 240
  • 385
13
votes
4 answers

How does the "#map(&proc)" idiom work when introspecting module classes?

Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end …
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
12
votes
2 answers

Using /proc/[pid]/pagemap

I am aware that there is a little information regarding the pagemap file here. But nobody seems to indicate how to reference entries in the file. Is it offset by virtual address? Can I take a virtual address VA and simply lseek to offset VA? Or…
11
votes
5 answers

Provide a proc to a method as a block

Let's say I have the following array: arr = [[5, 1], [2, 7]] and I want to find the minimum element, comparing the second element of the elements. The minimum element will be [5, 1] since 1 is less than 7. I can use the following code: arr.min…
Cristobal Viedma
  • 990
  • 1
  • 8
  • 20
11
votes
2 answers

What is the difference between a method and a proc object?

I know that methods in Ruby are not objects but procs and lambdas are. Is there any difference between them other than that? Because both we can pass around. What makes the proc objects different from a method? Method: 1.8.7-p334 :017 > def…
Kranthi
  • 1,377
  • 1
  • 17
  • 34
11
votes
5 answers

Why does Enumerable#detect need a Proc/lambda?

Enumerable#detect returns the first value of an array where the block evaluates to true. It has an optional argument that needs to respond to call and is invoked in this case, returning its value. So, (1..10).detect(lambda{ "none" }){|i| i == 11}…
Maxim Schmidt
  • 147
  • 12
1
2 3
68 69