Tagged Questions
0
votes
1answer
32 views
function in a Matrix manipulation
I'm trying to set an function as a matrix element and then I want this function to be called when matrix-multiplication is done..
Pseudo Example :
f = lambda {|x| Math.log(x) }
m1 = Matrix[ [f,0], ...
0
votes
2answers
60 views
How do I use Ruby's new lambda syntax?
Ruby has lambda syntax, so I can use the -> symbol:
a = 0
new -> { a < 5 } do
puts a
a += 1
end
This works very well, but when I try to do this:
match "/", to: -> { |e| [404, ...
2
votes
1answer
31 views
when calling instance_eval(&lambda) to pass current context got error 'wrong number of arguments'
To be clear - this code is running perfectly - code with proc
but if instead I change Proc.new to lambda, I'm getting an error
ArgumentError: wrong number of arguments (1 for 0)
May be this is ...
0
votes
1answer
35 views
In a function with two lambdas, when used with .call, how come only the last lambda is evaluated? (ruby clojures)
In the following code, I was reading on closures, but wondered what would happen if there was more than one proc object in the function. So when the function was .called, only the last lambda was ...
1
vote
4answers
43 views
Block does not recognise variable at first time
y = lambda { z }
z = 9
y.call
=> NameError: undefined local variable or method `z' for main:Object
y = lambda { z }
y.call
=> 9
I thought the idea of having a block was defer the execution ...
4
votes
1answer
64 views
How to convert method or lambda to non-lambda proc
As below, I can't call with wrong number of arguments a Proc created from a Method because it is a lambda, which is strict about the number of arguments.
# method with no args
def a; end
...
2
votes
1answer
71 views
Return statements inside procs, lambdas, and blocks
I am having a lot of trouble understanding how return works in blocks, procs, and lambdas.
For instance, in the following case, why does batman_ironman_proc work, while batman_yield throw an error?
...
4
votes
2answers
93 views
How to create this little DSL in Ruby?
My functions are:
def hello(str)
puts "hello #{str}"
end
def hello_scope(scope, &block)
# ???
end
I would like to temporarily augment a function within a block of my method.
In ...
0
votes
1answer
84 views
How do I properly scope a Ruby lambda?
I have this lambda:
echo_word = lambda do |words|
puts words
many_words = /\w\s(.+)/
2.times do
sleep 1
match = many_words.match(words)
puts match[1] if match
end
...
1
vote
2answers
89 views
Which one is a Ruby deprecated proc?
In the book Programming Ruby: The Pragmatic Programmers Guide by Dave Thomas with Chad Fowler and Andy Hunt, regarding the creation of Procs there is a footnote that states:
"There’s actually a ...
1
vote
1answer
78 views
Confused by use of Ruby 1.9 lambda literal in Rails ActiveRecord test suite
While trying to solve a barely-related problem, I ran across some code in this Rails ActiveRecord test file:
http://github.com/rails/rails/blob/master/activerecord/test/models/post.rb
belongs_to ...
0
votes
1answer
114 views
Ruby callback function to continue execution of another function
I'm using Pubnub to publish live messages from a Server to a client (browser page). When using Pubnub, one must abide by their message size constraints, sometimes resulting in the need to chunk the ...
0
votes
3answers
89 views
Ruby Block to Array
I'm trying to take a block argument in a method, and turn the contents (array of Symbols) and make it into an array. For example:
def sequence(&block)
# ?
end
sequence do
:foo,
:bar,
...
2
votes
3answers
90 views
How do I pass a lambda to Hash.each?
How can I pass a lambda to hash.each, so I can re-use some code?
> h = { a: 'b' }
> h.each do |key, value| end
=> {:a=>"b"}
> test = lambda do |key, value| puts "#{key} = #{value}" ...
1
vote
4answers
42 views
How do I use lambdas with arguments to collect an array?
I was curious how I may refactor this code:
array.collect{|x| x.some_method}.inject(:+) || 0
I have it about ten times in my code with different methods so I thought I should refactor, but how?
I ...