Unexpected Return (LocalJumpError)
You can't return
inside a block in Ruby*. The last statement becomes the return value, so you can just remove the return statements in your case:
p (1..8).collect{|denom|
(1...denom).collect{|num|
r = Rational(num, denom)
if r > Rational(1, 3) and r < Rational(1, 2)
1
else
0
end
}
}.flatten
*: You can inside lambda
blocks: lambda { return "foo" }.call # => "foo"
. It has to do with scoping and all that, and this is one of the main differences between lambda
blocks and proc
blocks. "Normal" blocks you pass to methods are more like proc
blocks.
Ruby - unexpected return (LocalJumpError)
You're not in a method. You cannot return
from there. If you want to exit early, use exit
.
Unexpected return error in Ruby
You can't use return
within an each
loop. You could use map
or collect
to return an array of last names like so:
people[:family].collect {|index| index[:last] if index[:first] == "John"}
=> ["Doe", nil]
You could also push your matches to an output variables like so:
output = []
people[:family].each {|index| output << index[:last] if index[:first] == "John"}
=> ["Doe"]
Using break
as was suggested, will return a string of the first match, but if you have more than one match, it will not work for you. See example below:
people = {
:family => [
{:first => "John", :last => "Doe"},
{:first => "Jane", :last => "Smith"},
{:first => "John", :last => "Galt"}
]
}
people[:family].each {|index| break index[:last] if index[:first] == "John"}
=> "Doe"
LocalJumpError: unexpected return on all db queries after ugrading to rails 4
The errors I got were not helpful but I found the problem.
Rails4 now requires that scopes are defined using procs or lambdas. see this documentation
Rails 4.0 requires that scopes use a callable object such as a Proc or
lambda:
scope :active, where(active: true)
becomes
scope :active, -> { where active: true }
By changing all of my model scopes to use lambdas, the issue was solved and I can query the db again.
It seems that even one outdated scope ruins all db calls.
Process throwing Unexpected return error
The problem is that return
can't be called from Ruby
procs. Just skip return
to avoid the error:
def proc_test(command)
proc = Proc.new do |command|
command_out = Mixlib::ShellOut.new(command)
command_out.run_command
command_out.stdout + command_out.stderr
end
proc.call(command)
end
implicit block passed gives local jump error
There are many differences between Lambda and Proc, as you can see in this post for example.
One of them is how the return
behaves in each case.
When you return
in a Proc, it will return from the method it is being called.
And when you return
in a Lambda it returns only to outside the lambda code.
The reason for the LocalJumpError
is simply because you are you're calling return
probably from your ruby console and there is no enclosing method to return. So if you surround your code with a method and call it, it will work.
def test
def my_function(&block)
p block.call
end
p my_function{ return "implicit block" }
end
test
=> "implicit block"
How do I do early return in ruby to reduce nested if?
Use next
instead of return
to exit the inner block early:
class A
def run
yield
end
end
a = A.new
a.run do
puts "ola"
next
puts "hello"
end
In order to use return
, it should be place directly inside the method body.
But when you pass a block to a function, with EventMachine.run do
and with Fiber.new{
, the code inside the block is not directly inside the function, but is passed as a parameter to a different function. Inside the block you cannot call return
, but you can exit early with next
.
I am guessing that the reason the designers decided this is because a return
inside the block would cause confusion whether the block would return, or the entire method.
Related Topics
Ruby Net::Http - Following 301 Redirects
How to Ensure That Ruby Uses an Openssl Not Vulnerable to Heartbleed
Rails - Sort by Join Table Data
Ruby: Dynamically Generate Attribute_Accessor
Ruby on Rails - "Add 'Gem SQLite3'' to Your Gemfile"
How to Save an Object to a File
How to Remove Permission Denied @ Rb_Sysopen - Gem Install Error
How to Uninstall Ruby on Rails on MAC Os X
How to Automate Chrome Request Blocking Using Selenium-Webdriver for Ruby
Get Current Ruby Process Memory Usage
Error Installing Nokogiri: Failed to Build Gem Native Extension & Libiconv Is Missing (Osx)
How to Cancel Scheduled Job with Delayed_Job in Rails
Convert a .Doc or .Pdf to an Image and Display a Thumbnail in Ruby
Where's the Best Place to Define a Constant in a Ruby on Rails Application