Handy resource for keywords: http://ruby-doc.org/docs/keywords/1.9/
This doc: http://bit.ly/1EqHNPH
def foo
42
end
foo def foo
return 42
puts 'wat'
end
foo return 'huh' def foo
yield
end
foo do
2
end def foo
yield
end
foo do
return
end def foo
yield
end
def bar
foo do
return
end
puts 'y hello thar'
end
bar def bar
yield + 1
end
def foo(&block)
bar(&block)
end
foo do
2
end def foo
(1..3).each do |x|
puts x
end
puts 'done'
end
foo def foo
(1..3).each do |x|
next if x == 1
puts x
end
puts 'done'
end
foo def foo
(1..3).each do |x|
break if x == 1
puts x
end
puts 'done'
end
foo def foo
(1..3).each do |x|
return if x == 1
puts x
end
puts 'done'
end
foo def foo
doubles = (1..3).map do |x|
2*x
end
doubles.reduce(:+)
end
foo def foo
doubles = (1..3).map do |x|
next 1000 if x == 1
2*x
end
doubles.reduce(:+)
end
fooInside a map, next with an argument specifies the value to be used for the current iteration.
def foo
doubles = (1..3).map do |x|
break 1000 if x == 1
2*x
end
doubles.reduce(:+)
end
foobreak terminates the iteration immediately and specifies the return value of the whole operation.
def foo
doubles = (1..3).map do |x|
redo if x == 1
2*x
end
doubles.reduce(:+)
end
fooredo causes the containing block to be re-executed with the same parameters again.
def foo
next
end def foo
yield + yield
end
foo do
2
end def foo
yield + yield
end
foo do
next 2
puts 'never happens'
endHere, next is acting a lot like a return statement but for the block.
def foo
yield + yield
end
foo do
break 2
puts 'never happens'
endbreak terminates the execution of the method that the block was passed to and returns the value.
def bar
yield + yield
end
def foo(&block)
result = bar(&block)
puts 'hi there'
result
end
foo do
break 2
endbreak unwinds all the way to the outside.
def foo
raise 'uh-oh'
puts 'never happens'
end
begin
foo
rescue
puts 'rescued exception'
end def foo
raise 'uh-oh'
puts 'never happens'
end
def bar
foo
rescue
puts 'rescued exception'
end
bar def foo
raise 'uh-oh'
puts 'never happens'
end
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
end class WatException < StandardError; end
def foo
raise WatException, 'this is a message'
puts 'never happens'
end
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
end class WatException < StandardError; end
def foo
method_that_doesnt_exist
raise WatException, 'this is a message'
puts 'never happens'
end
begin
foo
rescue WatException => e
puts "rescued exception #{e.inspect}"
endException is the root of the entire hierarchy. Some of its subclasses should NOT be rescued.
From http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy/ :
Exception
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError
ArgumentError
IOError
EOFError
IndexError
LocalJumpError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RuntimeError
SecurityError
SystemCallError
SystemStackError
ThreadError
TypeError
ZeroDivisionError
SystemExit
fatal
def foo
exit
rescue Exception => e
puts 'welcome to the ruby california'
end
fooThe rescue => e form is equivalent to rescue StandardError => e and avoids this behavior.
class WatException < StandardError; end
def foo
raise WatException, 'this is a message'
puts 'never happens'
end
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
raise
end class WatException < StandardError; end
def foo
raise WatException, 'this is a message'
puts 'never happens'
end
retries = 0
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
retries += 1
retry unless retries >= 3
endHandy for dealing with things like APIs that time out erratically.
def foo
raise 'uh-oh'
puts 'never happens'
end
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
ensure
puts "always happens"
end def foo
puts 'never happens except this time'
end
begin
foo
rescue => e
puts "rescued exception #{e.inspect}"
else
puts "no exception, yay"
end def foo
throw :huh
end
catch :huh do
foo
end
foo def foo
throw :huh, 'sekrit werd'
end
result = catch :huh do
foo
endOne place this is handy is Sinatra's halt method: https://github.com/sinatra/sinatra/blob/b8f95af6510311031e1c7979a3043c585fa39b58/lib/sinatra/base.rb#L940
def foo
2
end
result = catch :huh do
foo
end def foo
throw :huh, 12
end
result = catch :huh do
break 42
foo
end-
creating an exception object captures a stack trace, which can be expensive
-
rescue => ewill not swallowthrow :foo