Skip to content

Instantly share code, notes, and snippets.

View KamilLelonek's full-sized avatar
🏋️‍♂️
Do you even lift?

Kamil Lelonek KamilLelonek

🏋️‍♂️
Do you even lift?
View GitHub Profile
@KamilLelonek
KamilLelonek / random_string_generator.rb
Last active September 2, 2019 20:09
Ruby random string generator with particular length
class RandomStringGenerator
def without_numbers(length)
random_string(length, alphabet)
end
def with_numbers(length)
random_string(length, alphabet + numbers)
end
private
@KamilLelonek
KamilLelonek / sandwich_maker.rb
Created March 27, 2015 05:20
How to delegate methods in Ruby
class SandwichMaker
def make_me_a_sandwich
puts 'OKAY'
end
end
@KamilLelonek
KamilLelonek / array_capacity.rb
Created March 27, 2015 08:16
How many items does array have?
➜ ~ 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
@KamilLelonek
KamilLelonek / 1_wrapper.rb
Created March 27, 2015 10:14
How to delegate methods in Ruby
class LazyEmployee
def initialize(sandwich_maker)
@sandwich_maker = sandwich_maker
end
def make_me_a_sandwich
sandwich_maker.make_me_a_sandwich
end
private
@KamilLelonek
KamilLelonek / 1_missing.rb
Created March 27, 2015 10:20
How to delegate methods in Ruby
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
@KamilLelonek
KamilLelonek / 1_forwardable.rb
Last active August 18, 2021 20:04
How to delegate methods in Ruby
require 'forwardable'
class LazyEmployee
extend Forwardable
def initialize(sandwich_maker)
@sandwich_maker = sandwich_maker
end
def_delegators :@sandwich_maker, :make_me_a_sandwich
@KamilLelonek
KamilLelonek / 1_delegate.rb
Created March 27, 2015 12:12
How to delegate methods in Ruby
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
@KamilLelonek
KamilLelonek / 1_simple_delegator.rb
Last active March 28, 2017 21:30
How to delegate methods in Ruby
class LazyEmployee < SimpleDelegator
def initialize(sandwich_maker)
super
end
end
@KamilLelonek
KamilLelonek / curry.rb
Last active November 20, 2019 23:14
Currying functions in Ruby
[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)
@KamilLelonek
KamilLelonek / lambdas.rb
Last active August 29, 2015 14:21
Lambdas in Ruby
[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)>