Skip to content

Instantly share code, notes, and snippets.

class User < ActiveRecord::Base
def display_name
full_name || email_designator || "I haz no name nor email!"
end
def email_designator
return nil if email.blank?
@email_designator = email.split("@")[0]
end
@dreamr
dreamr / 1-with-lambdas.rb
Last active August 29, 2015 14:00
Laziness - or code as data!
shout =->(name) { "HELLO #{name.upcase}!" }
def greet(name, greeter)
greeter.call(name)
end
puts greet("Robert", shout) #=> HELLO ROBERT!
@dreamr
dreamr / vistalegre_scam.md
Created April 14, 2014 19:58
Do NOT rent from VisteLegre in Puerto Vallarta!

TERRIBLE EXPERIENCE!

I moved back to Vallarta in spring of 2014. The first apartment these guys rented to my finance and I turned out to be a crazy lady who accused us of having cats we do not have and kicking us out at 2am the first night we arrived.

After dragging all our belongings down over 100 steps and into a taxi and to a hotel, it was a night of hell.

The next day they moved us to a new apartment. We bought a washer machine and a very nice hammock. The building was shared by other short term vistalegre rental customers. In 3 months we had three gay old men (1 at a time) beneath us in the lower apartment.

Their smoke was constantly in my apartment, there was no separate meter on the gas so I shelled out all the gas money after it repeatedly ran out. Vistalegre did little to reimburse me for the other tenants half.

@dreamr
dreamr / gist:9711694
Created March 22, 2014 18:09
The rules and principles I try to follow
* Keep methods rediculously short
* Methods should do 1 thing
* Be idempotent when at all possible
* Leave objects open to extension, but closed to modification
* Don't modify objects unless constrained by memory operations
* Wrap Network and Disk and Database calls with encapsulation
* Dont mock what you dont own
* Write real unit tests, test inside the boundaries
* Write common path itegration tests, test the boundaries themselves
@dreamr
dreamr / dreamr_loves_beautiful_ruby.md
Last active December 29, 2015 00:29
An attempt to put the thoughts that go on in my head when I code to something that can be followed.

Keeping code readable and idiomatic

Design considerations:

  • Avoid imperative conditional logic
  • If you can do it with an enumerator, then just do it
  • Keep 90% of the overall methods idempotent
  • Wrap small sections of imperative shell around composite idempotent methods
  • Think in terms of behaviors and values, not objects and messages
  • Identify and classify data clumps (primitives that do not work as sub data)
require 'io/console'
# Adventure game in MatzLISP :)
COMMANDS = %w{quit}
DRAGON = File.read("dragon.txt")
get_input = -> { STDIN.gets.chomp }
prompt_user = ->(msg) { STDOUT.puts msg }
module Yahtzee
module ScoreCardUpdater
module_function
def update(game_card)
->(placement, value) {
game_card.update!(placement => value)
}
end
end
@dreamr
dreamr / gist:7421224
Last active December 28, 2015 01:29
A code challenge from a prospective client - name withheld to prevent other people cheating
@dreamr
dreamr / gist:7187332
Last active December 26, 2015 17:29
hangman in ruby lambdas!
require 'io/console'
# Some vars to give us data
dict = %W(ruby lambda functional)
game_word = dict[rand(dict.size)]
guesses = {
correct: [],
incorrect: []
}
max_guesses = 7
@dreamr
dreamr / after
Last active December 26, 2015 03:48
from mess to clean - showing a dev how to remove conditional logic
def calculate_return(last_trade)
send(:"calculate_#{recommendation.to_s}_return", last_trade)
end