Skip to content

Instantly share code, notes, and snippets.

View alexandru-calinoiu's full-sized avatar
💭
Crafting

Calinoiu Alexandru Nicolae alexandru-calinoiu

💭
Crafting
View GitHub Profile
@alexandru-calinoiu
alexandru-calinoiu / graph.rb
Created January 7, 2017 16:37
A very naive graph implementation in ruby
require 'pp'
class Graph
def initialize(graph = {})
@hash = graph.freeze
end
def add_vertex(node)
Graph.new(@hash.merge(Hash[node, { edges: {}.freeze }]).freeze)
end
class UserAddressBook
attr_accessor :user_id, :address_book_entry
def initialize(user_id, address_book_entry)
@user_id = user_id
@address_book_entry = address_book_entry
end
end
address_book1 = UserAddressBook.new(42, 'test')
@alexandru-calinoiu
alexandru-calinoiu / matchers.rb
Created November 7, 2016 15:25
Play around with dry matchers
require 'dry-matcher'
success_case = Dry::Matcher::Case.new(
match: -> value { value.first == :ok },
resolve: -> value { value.last }
)
failure_case = Dry::Matcher::Case.new(
match: -> value, *pattern {
value[0] == :err && (pattern.any? ? pattern.include?(value[1]) : true)
@alexandru-calinoiu
alexandru-calinoiu / dry-monads.rb
Created November 6, 2016 03:10
Play around with dry monads.
require 'dry-monads'
class Address
attr_reader :street
def initialize(street = nil)
@street = street
end
end
@alexandru-calinoiu
alexandru-calinoiu / custom_case.rb
Last active September 29, 2016 14:17
Playing with ruby case
class Success
def self.===(item)
item.status >= 200 && item.status < 300
end
end
class Empty
def self.===(item)
item.response_size == 0
end
@alexandru-calinoiu
alexandru-calinoiu / observable.rb
Created July 26, 2016 11:17
Some notes about observables based on a blog entry
require 'observer'
class Source
include Observable
def ping(int)
changed
notify_observers(int)
end
end
Laptop laptop = Laptop.new
laptop.listener = KeyBoardListener.new
class KeyBoardListener
def key_press(key_code)
...
end
end
RuntimeError - Called id for nil, which would mistakenly be 8 -- if you really wanted the id of nil, use object_id:
activesupport (3.2.13) lib/active_support/whiny_nil.rb:22:in `id'
app/views/vendor_area/coupons/_redeem_information.html.haml:1:in `_app_views_vendor_area_coupons__redeem_information_html_haml___1822889974911983243_178577280'
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.13) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
module UserFinders
extend ActiveSupport::Concern
def find_user_application(token)
user_applications.without(:application_errors).find(token)
end
end
Concurrency Level: 16
Time taken for tests: 111.600 seconds
Complete requests: 1024
Failed requests: 0
Write errors: 0
Total transferred: 583680 bytes
Total POSTed: 366592
HTML transferred: 246784 bytes
Requests per second: 9.18 [#/sec] (mean)
Time per request: 1743.743 [ms] (mean)