Skip to content

Instantly share code, notes, and snippets.

# Shoulda activemodel cheatsheet
# DB
should have_db_column(:title).of_type(:string).with_options(default: 'Untitled', null: false)
should have_db_index(:email).unique(:true)
# Associations
should belong_to :company
should have_one(:profile).dependent(:destroy)
should have_many(:posts).dependent(:nullify)
@maker-leo
maker-leo / question.rb
Created April 11, 2013 14:41
How to find all questions where the answers user_id IS NULL
includes(:answers)
.where("user_id IS NULL OR user_id != ?", user_id)
@maker-leo
maker-leo / dice_test.rb
Created March 27, 2013 17:08
Example of mocking in Ruby to take away our dependency on randomness. Don't forget to add mocha to your Gemfil
require 'minitest/autorun'
require 'mocha/setup'
require './lib/dice'
class DiceTest < MiniTest::Unit::TestCase
def setup
@one_dice = Dice.new(1)
@two_dice = Dice.new(2)
end
@maker-leo
maker-leo / gist:5253814
Created March 27, 2013 12:29
Setting up your computer
- Copy setup.zip to your Downloads folder
- Double click the folder to unzip the folder
- Open the folder, and then once inside:
@maker-leo
maker-leo / ruby-exercises.rb
Created March 19, 2013 17:35
Here's some exercises to go over tonight
#create your own methods
#create a method with one argument, and print it out
#create a method with two arguments
#create a method with one argument, which by default is nil
#create a method that returns two values, depending on if an argument is true or false
#take this hash, and use it to make this string. Make sure you use the hash twice,
#and use string interpolation i.e. "A string #{variable}"
hash = {:dario => "Dario", :leo => "Leo"}
@maker-leo
maker-leo / blocks.rb
Created March 19, 2013 16:10
Some examples of blocks, including our own implementation of select
def do_some_maths
puts yield(10)
puts yield(20)
end
do_some_maths do |num|
num * 20
end
do_some_maths do |number|
@maker-leo
maker-leo / arrays.rb
Created March 19, 2013 16:08
Some example of array methods in Ruby
# You can loop through a range
(1..10).each {|a| puts a }
first_ten = (1..10).to_a
# You can convert it into an array
puts first_ten.inspect
puts "selecting odd numbers"
@maker-leo
maker-leo / ruby-intro.rb
Created March 19, 2013 12:17
All the examples from our introduction to Ruby
def vader(evil = false)
# guard clause
return "something else" if evil == "luke"
# implicit return, last line executed
if evil
"i am your father and I hate you"
else
"i am your father and i love you"
end
@maker-leo
maker-leo / Ruby-universe.md
Created March 12, 2013 16:37
All the different parts of the Ruby universe

The Ruby universe

This is a brief introduction into the world of web development and Ruby. To view all the different gems listed by popularity check out the Ruby Toolbox.

Web frameworks

  • Rails - the most popular, although recently has been criticised for being a bit bloated - giving you more than you often need to build a simple web site
  • Sinatra - takes the opposite principal, more of a "micro framework" that starts with just enough to get you going, and you can then add Gems for more advanced stuff as and when you need it

Models

@maker-leo
maker-leo / Sublime-Text.markdown
Last active December 14, 2015 19:19 — forked from leoallen85/Sublime-Text.markdown
A guide to getting the most out of your new favourite editor Sublime Text

Sublime Text 2

Although you're free to use any editor we recommend Sublime. Here's how to get the most out of it

Setting up

Once you've installed Sublime there's a few things you should do to get things set up properly:

Package Control