🏋️♂️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> outer = 1 | |
=> 1 | |
[2] (pry) main: 0> increment = -> value { outer + value } | |
=> #<Proc:0x007fb94c87e148@(pry):2 (lambda)> | |
[3] (pry) main: 0> increment[2] | |
=> 3 | |
[4] (pry) main: 0> increment::(2) | |
=> 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> def factorial(number) | |
[1] (pry) main: 0* return 1 if number <= 1 | |
[1] (pry) main: 0* number * factorial(number - 1) | |
[1] (pry) main: 0* end | |
=> :factorial | |
[2] (pry) main: 0> factorial(4) | |
=> 24 | |
[3] (pry) main: 0> def factorial(number, accumulator = 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> { key: :val }.fetch(:key, raise('Key does not exist')) | |
RuntimeError: Key does not exist | |
[2] (pry) main: 0> { key: :val }.fetch(:key) { raise('Key does not exist') } | |
=> :val | |
[3] (pry) main: 0> Array.new(3, rand(10)) | |
=> [0, 0, 0] | |
[4] (pry) main: 0> Array.new(3) { rand(10) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> %w(ruby is awesome).map &:upcase | |
=> ["RUBY", "IS", "AWESOME"] | |
[2] (pry) main: 0> def shuffle_letters(word) | |
[2] (pry) main: 0* word.split(//).shuffle.join | |
[2] (pry) main: 0* end | |
=> :shuffle_letters | |
[3] (pry) main: 0> %w(ruby is awesome).map &method(:shuffle_letters) | |
=> ["ubyr", "is", "emasewo"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> a = 1 | |
=> 1 | |
[2] (pry) main: 0> b = 2 | |
=> 2 | |
[3] (pry) main: 0> def add(a, b) | |
[3] (pry) main: 0* a + b | |
[3] (pry) main: 0* end | |
=> :add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> def print_and_dump(string) | |
[1] (pry) main: 0* open('dump.txt', 'a') do |file| | |
[1] (pry) main: 0* puts string | |
[1] (pry) main: 0* file.puts string | |
[1] (pry) main: 0* end | |
[1] (pry) main: 0* end | |
=> :print_and_dump | |
[2] (pry) main: 0> print_and_dump('test') | |
test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def divide(dividend, divisor) | |
dividend / divisor | |
rescue ZeroDivisionError | |
p $!.message | |
end | |
def average(*values) | |
raise ArgumentError.new('No values given') if values.empty? | |
raise ArgumentError.new('Not all values are numbers') unless values.all? { |value| value.is_a? Numeric } | |
values.reduce(:+) / values.length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] (pry) main: 0> sum = -> (a, b) { a + b } | |
=> #<Proc:0x007fa9c176e950@(pry):59 (lambda)> | |
[2] (pry) main: 0> increment = -> c { sum.(1, c) } | |
=> #<Proc:0x007fa9c173c900@(pry):60 (lambda)> | |
[3] (pry) main: 0> increment[3] | |
=> 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A | |
extend self | |
def a | |
puts 'a' | |
end | |
end | |
A.a # => 'a' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module B | |
module_function | |
def b | |
puts 'b' | |
end | |
end | |
B.b # => 'b' |