Questions tagged [proc-object]

47 questions
6
votes
4 answers

Why can procs be invoked with === in ruby 1.9?

This article mentions 4 ways to invoke procs in ruby 1.9, and === is one of them. I don't understand why this would be done this way at all. Does it have any relationship to the normal meaning of === (asking if the two objects are the same…
John Bachir
  • 22,495
  • 29
  • 154
  • 227
6
votes
2 answers

How do we copy singleton methods between different Ruby classes?

I am trying to define a class with methods, and a class lacking those methods, and then allowing an object of the latter class to 'learn' the methods from an instance of the former class. This is my attempt (Ruby 1.9.2) - it breaks (at the line…
Anthony
  • 61
  • 1
5
votes
1 answer

Are there any "simple" explanations of what procs and lambdas are in Ruby?

Are there any "simple" explanations of what procs and lambdas are in Ruby?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
4
votes
2 answers

How do I using instance variables from within a lambda/Proc defined in a class variable?

I wrote the following code: class Actions def initialize @people = [] @commands = { "ADD" => ->(name){@people << name }, "REMOVE" => ->(n=0){ puts "Goodbye" }, "OTHER" => ->(n=0){puts "Do Nothing" } } end def…
Srgrn
  • 1,770
  • 16
  • 30
4
votes
2 answers

Ruby Lambda vs. Proc LocalJumpError

Ruby and StackOverflow newb here working my way through Ruby and ran into my first major roadblock. I'm having a really hard time wrapping my head around Procs and Lambdas. Here is the code I'm working with. def procBuilder(message) Proc.new{ puts…
Tim Lindsey
  • 727
  • 1
  • 7
  • 18
4
votes
2 answers

tcl: wrap a proc of the same name

I want to replace the definition of "proc N" with a proc of the same name and calling conventions, but with a little extra error detection code. In python I could do what I want like below, but I don't have any grasp of how namespaces and function…
bukzor
  • 37,539
  • 11
  • 77
  • 111
4
votes
3 answers

Trouble with setting attributes

I have an item ActiveRecords and I am trying to set a default value ("Test item") for each of them using a block. In this expression: list = {"type1", "type2", "type3", "type4", "..."} list.each { |name| @item.attributes["#{name}"] = "Test item"]…
user502052
  • 14,803
  • 30
  • 109
  • 188
4
votes
3 answers

How do I print out a tcl proc?

Given a simple tcl proc like proc foo {a b} {puts "$a $b"} What tcl command can I use to print out the procedure foo ... that is I want the text of the proc back ... For instance: % proc foo {a b} {puts "$a $b"} % foo a b a b % puts $foo …
Xofo
  • 41
  • 1
  • 2
3
votes
1 answer

What are the advantages of proc functions to methods

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…
fl00r
  • 82,987
  • 33
  • 217
  • 237
3
votes
3 answers

TCL - return variable vs upvar and modify

Would like to take an advice from TCL professionals for best practice. Say you want to construct a list with a specific data by using a proc. Now which is the best way? proc processList { myList } { upvar $myList list_ #append necessary data…
Narek
  • 38,779
  • 79
  • 233
  • 389
3
votes
2 answers

Proc.new in Ruby: when do I need to use it?

The date_validator in its examples has a comment: Using Proc.new prevents production cache issues Does it mean, that everywhere in my code, where I use current time related methods (Time.now, 1.day.since(Time.zone.now), etc.) I should surround them…
krn
  • 6,715
  • 14
  • 59
  • 82
3
votes
1 answer

Referencing a local variable from a Proc defined in another scope

I want to create an instance method which varies its behaviour with return value of another method depending on a implementation of its overwrites in a polymorphic manner. For example, the following class is assumed to be extended and pricing_rule…
suzukimilanpaak
  • 751
  • 2
  • 8
  • 20
3
votes
1 answer

Behavior of 'return' keyword within a ruby block

Can someone explain the behavior of the following def iterate return yield return "end of iterate" end def test_iterate assert_equal( "end of iterate", iterate { return "end of block" } ) assert_equal( "end of block", iterate { "end of…
3
votes
2 answers

Inconsistency of arity between Hash.each and lambdas

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 …
SooDesuNe
  • 9,880
  • 10
  • 57
  • 91
3
votes
1 answer

Are there Ruby precedence issues with using Proc.call vs. Proc.[]?

Recently I was having a discussion with a friend about Ruby's Proc. You can call a Proc in one of several ways. One way is to invoke Proc.call: p = Proc.new { |x| "hello, #{x}" } p.call "Bob" => "hello, Bob" Another is to use braces, Proc.[]: p…
John Feminella
  • 303,634
  • 46
  • 339
  • 357