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 / closures.rb
Created May 16, 2015 12:45
Closures example in Ruby
[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
@KamilLelonek
KamilLelonek / factorial.rb
Last active August 29, 2015 14:21
Recursion example in Ruby
[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)
@KamilLelonek
KamilLelonek / lazy.rb
Last active August 29, 2015 14:21
Lazy evaluation example in Ruby
[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) }
@KamilLelonek
KamilLelonek / high_order.rb
Created May 16, 2015 13:34
High-order functions example in Ruby
[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"]
@KamilLelonek
KamilLelonek / referential_transparency.rb
Created May 16, 2015 13:37
Referential Transparency example in Ruby
[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
@KamilLelonek
KamilLelonek / dirty_function.rb
Last active August 29, 2015 14:21
Ruby function example
[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
@KamilLelonek
KamilLelonek / partial_function.rb
Created May 17, 2015 19:25
Partial function example in Ruby
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
@KamilLelonek
KamilLelonek / partial_application.rb
Created May 17, 2015 19:31
Partially applied function example in Ruby
[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
@KamilLelonek
KamilLelonek / extend_self.rb
Last active October 30, 2017 00:34
Ruby `extend self` example
module A
extend self
def a
puts 'a'
end
end
A.a # => 'a'
@KamilLelonek
KamilLelonek / module_function.rb
Created May 23, 2015 18:10
Ruby `module_function` example
module B
module_function
def b
puts 'b'
end
end
B.b # => 'b'