Skip to content

Instantly share code, notes, and snippets.

@JeffCohen
Created May 11, 2012 16:09
Show Gist options
  • Save JeffCohen/2660678 to your computer and use it in GitHub Desktop.
Save JeffCohen/2660678 to your computer and use it in GitHub Desktop.
Fun with Ruby classes
# Open up a Rails app that has at least one model
# Create a file named config/initializers/sample.rb
# Paste this code into it
# Open up your console
# Try to figure out how to call these methods
# Try to figure out how they work
class ActiveRecord::Base
def self.sample
offset(rand(0...count)).first
end
def self.[](id)
find_by_id(id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment