The Proc behave different in different scope - ruby-on-rails

I am new to ruby and start learning ruby, and i came to this proc return concept where i completely confused how the proc is returning differently.
I am attaching my code here for the reference.
I did google search too but couldn't get my answer if anyone could help please.
def call_proc
puts "Before proc"
my_proc = Proc.new { return 2 }
my_proc.call
puts "After proc"
end
def proc_call
def inside_call
my_proc = Proc.new {return 4}
end
proc = inside_call
proc.all
end

I can't see the difference in those 2 pieces of code ... However it all appears to be working the way it is supposed to. Procs work the same way no matter how you call them.
see: https://www.codecademy.com/learn/learn-ruby/modules/learn-ruby-blocks-procs-and-lambdas-u/cheatsheet
and
https://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/#Lambdas_vs_Procs
Your code (and my output):
def call_proc
my_proc = Proc.new { return 2 }
puts "Before proc"
my_proc.call
puts "After proc"
end
def proc_call
def inside_call
my_proc = Proc.new {return 4}
end
proc = Proc.new {return 4}
puts "Before proc"
proc.call
puts "After proc"
end
Output:
2.7.2 :112 > call_proc
Before proc
=> 2
2.7.2 :113 > proc_call
Before proc
=> 4
2.7.2 :114 >
In comparison, where you want to carry on after your Proc, you might be better off performing a Lambda:
def call_lambda
my_lambda = -> { return 17 }
puts "Before lambda"
puts my_lambda.call
puts "After lambda"
end
(note the puts before the call, and the return value of nil)
2.7.2 :122 > call_lambda
Before lambda
17
After lambda
=> nil

First of all, there are no nested methods in Ruby. The 2nd part of your code is equivalent to:
def inside_call
Proc.new { return 4 }
end
def proc_call
proc = inside_call
proc.call
end
Calling proc_call (still) results in a LocalJumpError. This is because return within a proc will always attempt to return from the method the proc was defined in (i.e. from inside_call). From the docs:
In non-lambda procs, return means exit from embracing method (and will throw LocalJumpError if invoked outside the method);

Related

Ruby - retry function with different params

I'd like to retry a function with different params depending on the result of the first iteration:
Giving a retry function like follow:
def retry_on_fail(**args)
yield
rescue StandardError => e
args = args.merge(different_param => true) if e.class == `specific_error`
retry
Is there a way to do so? I didn't find it yet...
Thanks!
You can yield however many times you want in a method and the trick is really passing the arguments to the block:
# given
class SpecificError < StandardError; end
def retry_on_fail(**args)
yield(args)
rescue SpecificError
yield(args.merge(different_param: true))
end
retry_on_fail do |args|
raise SpecificError if args.empty?
args
end
# returns { different_param: true }
There is also a slight differnce here flow wise - retry runs the whole method from the top and this will just call the block again. If thats what you want you could do:
def retry_on_fail(**args)
yield(args)
rescue SpecificError
args.merge!(different_param: true)
retry
end
But this has the potential to create an endless loop if the block raises the same exception again.
Try this
def retry_on_fail(**args)
rescue_args = args
begin
yield(rescue_args)
rescue StandardError => e
rescue_args = rescue_args.merge(different_param => true) if e.class == `specific_error`
retry
end
end

yield to an anonymous block two functions up

there is probably a simple way to do this.
I'm trying to refactor something like the following
def foo(baz)
baz.update_first
if baz.has_condition?
yield baz.val if block_given?
baz.a
else
baz.b
end
end
called like
foo(baz) {|b| b.modify}
to something like
def foo(baz)
baz.update_first
bar(baz) {|i| yield i if block_given? }
end
def bar(baz)
if baz.has_condition?
yield baz.val if block_given?
baz.a
else
baz.b
end
end
Will that work? How?
I think it will, but I'd appreciate a clear explanation of how yielding inside a block works... reading through proc.c and vm.c and a relevant git commit in the ruby source code , I think when bar is called in foo it executes until it yields, and then you walk up the frame stack to the local environment pointer for block defined in foo, which is called, where the yield walks up to the block foo is called with, executes it, and then you are back in bar. Is that correct? Is there a better way to do this?
This feels a little weird to me, like inverting control, and it requires foo to know about baz more then I'd like, but I unfortunately can't simply pass a proc or lambda in this code.
I think maybe the concept of yield will be more clear if you look at an alternative syntax, which is converting the bloc to a proc argument.
For example, the following examples are the same
def my_each(arr)
arr.each { |x| yield x }
end
def my_each(arr, &blk)
arr.each { |x| blk.call(x) }
end
# Both are called the same way
my_each([1,2,3]) { |x| print x }
# => 123
When using yield, the variable is available in the method without declaring it in the parameters list. Prepending an & sign to a parameter converts it to a proc, so in the method it can be run with .call.
Here's an example of providing a block to one method then executing it two scopes in:
def method_a(number, &blk)
method_b do
method_c do
blk.call(number)
end
end
end
def method_b(&blk)
blk.call
end
def method_c(&blk)
blk.call
end
method_a(1) { |num| puts num + 1 }
# => 2
Note that blk is not a magic word - you can name the variable whatever you want.
Here's the same thing with yield:
def method_a(number)
method_b do
method_c do
yield number
end
end
end
def method_b
yield
end
def method_c
yield
end
method_a(1) { |num| puts num + 1 }
# => 2
I think using the &blk syntax is clearer because it assigns a variable to the proc. Just because a proc is used in the method doesn't mean you have to ever run Proc.new. The block is automatically converted to a proc.

Ruby - collection of methods

I have inherited a bunch of methods that are not wrapped by any classes or modules, and are just listed in an .rb file. This was made possible due to the file being used inside a Cucumber test suite. I want to take a collection of all these methods and iterate over each method call, doing some work on each one as they are called.
EX:
def call_all_methods
method1
method2
method3(true)
method3(false)
method4('Y', true)
method4('N', true)
method4('Y', false)
method4('N', false)
end
What i want to be able to do is wrap these all in an array and call them individually with a begin/rescue block around them
$all_methods.each do |method|
begin
method.call
rescue Exception1
handle_exception1
rescue Exception2
handle_exception2
end
end
I've tried putting them all in an array using %w
call_all_methods = %w(...)
and that works but it makes the methods ugly to look at in the IDE
I've tried doing a readlines on the file, but the methods get executed while the file is being read.
I could create methods to wrap each call, but then I have a method to call another method (one line) which isn't right either.
I have looked at Ruby: methods as array elements - how do they work? but neither of those solutions seemed like good solutions for what I'm trying to do, as it would dirty the code
If I understand what you're asking correctly, you could just wrap those methods in a class.
class MyMethods
# all those methods that you have in that file
end
You could then list them all by doing
all_methods = MyMethods.instance_methods(false)
To execute them, you can do all_methods.each {|m| MyMethods.new.send(m)}
You could do something like this:
def execute_several(arr)
arr.each do |method, *args|
begin
v = send(method, *args)
puts "for method '#{method}': #{v}"
rescue ArgumentError => e
puts "for method '#{method}': #{e.message}"
end
end
end
arr = [
[:class],
[:rand, 20],
[:Integer, "20"],
[:Integer, 'cat']
]
execute_several(arr)
# for method 'class': Object
# for method 'rand': 17
# for method 'Integer': 20
# for method 'Integer': invalid value for Integer(): "cat"
Here's an example of how that would be done within a class:
class Array
def execute_several(arr)
arr.each do |method, args|
begin
v = args ? send(method, args) : send(method)
puts "for method '#{method}': #{v}"
rescue TypeError => e
puts "for method '#{method}': #{e.message}"
end
end
end
end
arr = [
[:reverse],
['first'],
[:&, [2,3,4]],
[:|, 'cat']
]
[1,2,3].execute_several(arr)
# for method 'reverse': [3, 2, 1]
# for method 'first': 1
# for method '&': [2, 3]
# for method '|': no implicit conversion of String into Array
I ended up making an array of procs

How can you detect when "break" is called in the callers Proc?

I'm writing a library that iterates over a set and calls the caller's proc for every item in the set. Example:
def self.each(&block)
# ... load some data into results_array
results_array.each do |result|
status = block.call(result)
# how do I know to call break if the user calls break?
break if status == false
end
end
Currently, as you can see in my code, I inspect the "last expression evaluated" in order to break. This seems bug-prone as the end-user may have a perfectly valid reason for their last expression evaluating to false. The more appropriate thing would be to detect the caller using "break".
How do I know to call break if the user calls break?
If you use yield instead of block.call, you can use the difference in behavior between next and break. As an example:
def each(&block)
puts "before"
result = yield
puts "after"
result
end
each do
puts "hello"
next
end
# Result:
# before
# hello
# after
each do
puts "hello"
break
end
# Result:
# before
# hello
As you can see, when you use next inside a block, the control is given back to the function calling the block. If you use break however, the calling function will return immediately with nil as a return value. You could now exploit this behavior with some trick:
def each(&block)
# ...
results_array.each do |result|
block_result = yielder(result, &block) || {:status => :break, :value => nil}
if block_result[:status] == :break
# block has called break
#...
else
# block has called either next or the block has finished normally
#...
end
end
end
def yielder(*args, &block)
value = yield *args
{:status => :normal, :value => value}
end
This works because here, the yielder function returns either nil in case the block called break or a hash with a status and the return value of the block. You can thus differentiate between a valid result (which is always different from nil) and an exceptional result which is always nil.
This should work, unless I don't understand what you are trying to do:
def each(&block)
# ... load some data into results_array
results_array= [1, 2, 3]
results_array.each do |result|
block.call(result)
end
end
each do |result|
puts result
break
end

blocks in silly blocks rspec testing

I had the following tests given to me as an exercise:
require "silly_blocks"
describe "some silly block functions" do
describe "reverser" do
it "reverses the string returned by the default block" do
result = reverser do
"hello"
end
result.should == "olleh"
end
it "reverses each word in the string returned by the default block" do
result = reverser do
"hello dolly"
end
result.should == "olleh yllod"
end
end
describe "adder" do
it "adds one to the value returned by the default block" do
adder do
5
end.should == 6
end
it "adds 3 to the value returned by the default block" do
adder(3) do
5
end.should == 8
end
end
describe "repeater" do
it "executes the default block" do
block_was_executed = false
repeater do
block_was_executed = true
end
block_was_executed.should == true
end
it "executes the default block 3 times" do
n = 0
repeater(3) do
n += 1
end
n.should == 3
end
it "executes the default block 10 times" do
n = 0
repeater(10) do
n += 1
end
n.should == 10
end
end
end
I was able to solve them with the following code:
def reverser
k = []
x = yield.split(" ")
x.each do |y|
n = y.reverse
k.push(n)
end
m = k.join(" ")
m
end
def adder(num=1, &block)
block.call + num
end
def repeater(num=1, &block)
for i in (1..num) do
block.call
end
end
However I some of these concepts I do not understand all that well. For example:
What exactly does the & symbol in the &block parameter mean?
Similarly what is block.call and where is the actual block object I am assuming its calling?
Could I theoretically use another method on block if I wanted to achieve something else?
Also where can I learn a bit more about blocks
This exercise was a bit above my current knowledge.
It means "this is the block parameter". You are not bound to calling it &block, so there needs to be a way to separate it from the other arguments. The same notation is used to pass arguments to a function as block as opposed to normal arguments (see below)
block.call is exactly the same thing as yield. The difference is that you can use block to access the block itself without calling it immediately. For example, you could store the block for later execution. This is a common pattern known as lazy evaluation.
Yes, you can also pass different things than a do/end block as the &block parameter. See below for some examples.
#UriAgassi gave you an excellent link.
Here are some other things you can pass as block argument. First, just a simple method that takes a block for demonstration:
def reverser(&block)
block.call.reverse
end
You can now pass a standard block
reverser do
"hello"
end
#=> "olleh"
Or, in alternative block syntax, used for inline style
reverser { "hello" }
#=> olleh
You can also pass a lambda or proc, which is similar to a block.
By using the &block notation you can pass a variable as block argument:
my_block = lambda { "hello world!" }
reverser(&my_block)
#=> "!dlrow olleh"
Or, in alternative lambda syntax
my_block = -> { "hello world!" }
reverser(&my_block)
#=> "!dlrow olleh"
You can even take an existing method and pass it as block argument
here you can see the great advantage of blocks: They are evaluated
when block.call is executed, not when the code is loaded. Here this
means that the string will change every time accordingly.
def foobar
"foobar at #{Time.now}"
end
reverser(&method(:foobar))
#=> "0020+ 15:42:90 02-50-4102 ta raboof"
#=> "0020+ 31:52:90 02-50-4102 ta raboof"
You can do cool stuff with this, for example:
[1, 2, 3].each(&method(:puts))
1
2
3
#=> [1, 2, 3]
But remember not to overdo it, Ruby is all about expressive and readable code. Use these techniques when they enhance your code, but use simpler ways if possible.
Finally, here is also an example of lazy evaluation:
class LazyReverser
def initialize(&block)
#block = block
end
def reverse
#block.call.reverse
end
end
reverser = LazyReverser.new do
# some very expensive computation going on here,
# maybe we do not even need it, so lets use the
# lazy reverser!
"hello dolly"
end
# now go and do some other stuff
# it is not until later in the program, that we can decide
# whether or not we even need to call the block at all
if some_condition
reverser.reverse
#=> "yllod olleh"
else
# we did not need the result, so we saved ourselves
# the expensive computation in the block altogether!
end

Resources