Skip to content

Instantly share code, notes, and snippets.

id schedule_id start_datetime end_datetime deleted_at created_at
1 1 2018–01–12 08:00:00 2018–01–12 09:00:00 2018–01–11 00:00:00
1 1 2018–01–12 08:00:00 2018–01–12 09:00:00 2018–01–11 00:00:00
@peiyee
peiyee / dynamic_method.rb
Created January 11, 2017 02:24
Dynamic methods
class ComputerDumberClass
def initialize(computer_id, data_source)
@id = computer_id
@data_source = data_source
end
def mouse
info = @data_source.get_mouse_info(@id)
price = @data_source.get_mouse_price(@id)
result = "Mouse: #{info} ($#{price})"
@peiyee
peiyee / monkeypatch.rb
Created January 11, 2017 02:22
Object model
# Monkeypatch
# original object class
class ObjectModel
def to_alphanumeric(s)
s.gsub(/[^​\w\s​]/, '')
end
end
# monkey patch in string class
class String
class Klass
def initialize(method_name)
@method_name = method_name
end
def self.this_is_my_name
puts self
end
def this_is_my_name
@peiyee
peiyee / rails_load_path_tips.md
Created July 13, 2016 02:19 — forked from maxim/rails_load_path_tips.md
How to use rails load paths, app, and lib directories.

In Rails 3

NOTE: This post now lives (and kept up to date) on my blog: http://hakunin.com/rails3-load-paths

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

class Node
attr_accessor :value, :left_node, :right_node
def initialize(value,l=nil,r=nil)
@value = value
@left_node = l
@right_node = r
end
def has_child?
return !@left_node.nil? && !@right_node.nil?
end