Skip to content

Instantly share code, notes, and snippets.

@phuibonhoa
phuibonhoa / gist:243f956bdc76786f7db7
Created August 21, 2014 10:38
Don't use self, unless it is required.
# Only use self when it is necessary.
# reader methods do not require self unless a local variable is declared with the same name (which is a bad idea anyway)
# setter methods do require self, otherwise you are creating a local variable
# Minimal use of self (only writes use 'self')
class Person
attr_accessor :name, :date_of_birth
def initialize(name:, date_of_birth:)
@phuibonhoa
phuibonhoa / gist:4a3da3649d0671f0164e
Created August 21, 2014 10:33
Use named args for boolean parameters.
# Boolean paramters easily lose their context/meaning. Ex. car.drive(true) # what is true?
#
# (1) If you have booleans, use named args.
# (2) If you dont have named args, use an options hash with fetch.
# (3) At the very worse, have the method take a meaningful string/symbol and determing truthiness from the passed value.
# BAD. Hard to tell what the boolean means when calling the method.
#
# load_video('kitty-yawn.mp4', true)
@phuibonhoa
phuibonhoa / gist:3ed6770aeb5c756088c2
Last active August 29, 2015 14:05
use attr_reader methods instead of @instance_variables internally within a class
# attr_reader methods
# √ 1 common way to access attribute value
# √ Easier to refactor into a calculated/facade attribute
# attr_reader methods
class Person
attr_reader :name, :date_of_birth
def initialize(name:, date_of_birth:)
@phuibonhoa
phuibonhoa / gist:974aae201421f0177c92
Created August 21, 2014 10:06
use memoized attributes instead of process methods
# Memoized facade attribute
# √ Lazy loaded
# √ Better encapsulated / more 'atomic'. Processing introduces unnecessary depencies at different times of the life cycle of the object.
# √ More idiomatic mindset to think of these are 'attributes' on an object, than results of a calculation
# Memoized attribute
class RecommendationCalculator
attr_reader :user
@phuibonhoa
phuibonhoa / cf_chat.css
Created October 24, 2013 05:44
Custom Propane CSS Save file to: ~/Library/Application\ Support/Propane/styles/cf_chat.css Preview: http://d.pr/i/mtv6
#upload_target {
display:none ! important;
}
div.bottom {
padding:0px ! important;
margin:0px ! important;
background-image: none ! important;
}
div#last_message{
display:none ! important;
@phuibonhoa
phuibonhoa / customer_test.rb
Created March 30, 2011 01:47
ex: execute in shoulda tests
context ".rent book with quantity" do
setup do
@book = Factory(:book, :quantity => 1)
@customer = Factory(:customer)
end
execute do
@customer.rent(@book)
end