🏋️♂️
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
class RandomStringGenerator | |
def without_numbers(length) | |
random_string(length, alphabet) | |
end | |
def with_numbers(length) | |
random_string(length, alphabet + numbers) | |
end | |
private |
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
class SandwichMaker | |
def make_me_a_sandwich | |
puts 'OKAY' | |
end | |
end |
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
➜ ~ pry | |
[1] (pry) main: 0> a = [1, 2, 3] | |
=> [1, 2, 3] | |
[2] (pry) main: 0> a.count | |
=> 3 | |
[3] (pry) main: 0> a.size | |
=> 3 | |
[4] (pry) main: 0> a.length | |
=> 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
class LazyEmployee | |
def initialize(sandwich_maker) | |
@sandwich_maker = sandwich_maker | |
end | |
def make_me_a_sandwich | |
sandwich_maker.make_me_a_sandwich | |
end | |
private |
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
class LazyEmployee | |
def initialize(sandwich_maker) | |
@sandwich_maker = sandwich_maker | |
end | |
def method_missing(method, *args) | |
if sandwich_maker.respond_to?(method) | |
sandwich_maker.send(method, *args) | |
else | |
super |
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
require 'forwardable' | |
class LazyEmployee | |
extend Forwardable | |
def initialize(sandwich_maker) | |
@sandwich_maker = sandwich_maker | |
end | |
def_delegators :@sandwich_maker, :make_me_a_sandwich |
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
require 'active_support/core_ext/module/delegation' | |
class LazyEmployee | |
def initialize(sandwich_maker) | |
@sandwich_maker = sandwich_maker | |
end | |
delegate :make_me_a_sandwich, to: :sandwich_maker | |
private |
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
class LazyEmployee < SimpleDelegator | |
def initialize(sandwich_maker) | |
super | |
end | |
end |
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> add = -> (a, b) { a + b } | |
=> #<Proc:0x007ffde11d72c0@(pry):1 (lambda)> | |
# Call proc with two arguments | |
[2] (pry) main: 0> add.(1, 2) | |
=> 3 | |
# Call proc with one argument | |
[3] (pry) main: 0> add.(1) | |
ArgumentError: wrong number of arguments (1 for 2) |
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> l = lambda { puts "I'm in lambda" } | |
=> #<Proc:0x007ffde32af6f0@(pry):9 (lambda)> | |
[2] (pry) main: 0> l.call | |
"I'm in lambda" | |
=> nil | |
[3] (pry) main: 0> l = -> name { puts "Hi #{name}!" } | |
=> #<Proc:0x007ffde3312840@(pry):14 (lambda)> |