Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / benchmark.rb
Created August 6, 2014 11:29
Benchmarking Memoizing of Ruby Methods
require 'benchmark'
class MyClassWithStuff
DEFAULT_VALS = { one: '1' }
def memoized_fetch
@value ||= DEFAULT_VALS[:one]
end
@Bodacious
Bodacious / example_controler.rb
Last active August 29, 2015 14:13
controller/views/intsance varialbes
# Bad:
class OrdersController
exposes :orders
def index
@orders = Order.complete
end
def pending
@Bodacious
Bodacious / example.rb
Created January 21, 2015 12:50
Exposable attribute
class OrdersController
def index
end
def pending
end
private
@Bodacious
Bodacious / array.rb
Created February 5, 2015 16:07
Ruby core extension for adding items conditionally to Arrays
class Array
# Inserts an object to the Array if condition is true
#
# object - The object to insert.
# condition - The condition to evaluate.
#
# Example:
#
# = content_tag(:div, class: ["user-username"].insert_if("active", @user.active)) do
@Bodacious
Bodacious / .gitconfig
Created March 10, 2015 19:37
My GitConfig file
[diff]
tool = diff
algorythm = minimal
[color]
branch = auto
diff = auto
status = auto
interactive = auto
ui = true
pager = true
#import <AddressBook/AddressBook.h>
#import <Accounts/Accounts.h>
#import <Social/Social.h>
@interface ContactsDiscovery()
@property (nonatomic, retain) ACAccountStore* accounts;
@property (nonatomic, retain) ACAccountType* faceBookAccountType;
@property (nonatomic, retain) ACAccount* faceBookAccount;
@property (nonatomic, retain) id addressBook;
@end
@Bodacious
Bodacious / calendar.rb
Last active August 29, 2015 14:20
Calendar with Event object
require 'time'
require 'date'
class Invitee
end
class Event
attr_accessor :name
attr_accessor :date
attr_accessor :starts_at
@Bodacious
Bodacious / multiple_conditionals.rb
Created May 13, 2015 12:28
When you should nest multiple conditionals in Ruby
def should_nest_multiple_conditionals?
x = rand(10).to_i
if Time.now.strftime("%A") != "Friday"
if current_client.is_large_faceless_conglomerate?
return true
elsif Time.now.strftime("%A") == "Friday"
return true
end
elsif LocalWeather.new("Edinburgh").sunny?
@Bodacious
Bodacious / benchmark.rb
Last active August 29, 2015 14:21
Ruby Benchmark: What's the fastest way to check the start of a String in Ruby?
require 'benchmark'
TIMES = 1000000
STRING = "this is a long and bad-ass string"
Benchmark.bmbm do |test|
test.report("Index") do
TIMES.times { STRING.index("this is") }
@Bodacious
Bodacious / Explanation.md
Last active August 29, 2015 14:22
Ruby: Passing Method objects into methods that accept blocks is SLOWER than procs

Explanation

So I thought I was being clever, writing my Rails controller actions like this:

def create
  set_post_from_post_id
  set_comment_as_new
  respond_to do |format|
    comment.save