Last active
October 5, 2015 02:03
-
-
Save Algogator/f309eb93abee91b72f52 to your computer and use it in GitHub Desktop.
Ruby Cheat Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ActiveRecord::Base | |
module ActiveRecord | |
class Base | |
end | |
end | |
module Math | |
PI = 3.141 # ... | |
end | |
puts Math::PI | |
# This is a comment | |
=begin | |
This is a multiline comment | |
No-one uses them | |
You shouldn't either | |
=end | |
# Numbers are objects | |
3.class #=> Fixnum | |
# Arithmetic is just syntactic sugar | |
# for calling a method on an object | |
1.+(3) #=> 4 | |
10.* 5 #=> 50 | |
nil.class #=> NilClass | |
true.class #=> TrueClass | |
false.class #=> FalseClass | |
# `do_something_else` only called if `do_something` succeeds. | |
do_something() and do_something_else() | |
# `log_error` only called if `do_something` fails. | |
do_something() or log_error() | |
# By convention, use snake_case for variable names | |
snake_case = true | |
# Use descriptive variable names | |
path_to_project_root = '/good/name/' | |
path = '/bad/name/' | |
# Symbols (are objects) | |
# Symbols are immutable, reusable constants represented internally by an | |
# integer value. They're often used instead of strings to efficiently convey | |
# specific, meaningful values | |
:pending.class #=> Symbol | |
# Like arithmetic, [var] access | |
# is just syntactic sugar | |
# for calling a method [] on an object | |
array.[] 0 #=> 1 | |
array.[] 12 #=> nil | |
for counter in 1..5 | |
puts "iteration #{counter}" | |
end | |
# HOWEVER, No-one uses for loops. | |
# Instead you should use the "each" method and pass it a block. | |
# A block is a bunch of code that you can pass to a method like "each". | |
# It is analogous to lambdas, anonymous functions or closures in other | |
# programming languages. | |
# | |
# The "each" method of a range runs the block once for each element of the range. | |
# The block is passed a counter as a parameter. | |
# Calling the "each" method with a block looks like this: | |
(1..5).each do |counter| | |
puts "iteration #{counter}" | |
end | |
(1..5).each { |counter| puts "iteration #{counter}" } | |
counter = 1 | |
while counter <= 5 do | |
puts "iteration #{counter}" | |
counter += 1 | |
end | |
# cases can also use ranges | |
grade = 82 | |
case grade | |
when 90..100 | |
puts 'Hooray!' | |
when 80...90 | |
puts 'OK job' | |
else | |
puts 'You failed!' | |
end | |
#=> "OK job" | |
# exception handling: | |
begin | |
# code here that might raise an exception | |
raise NoMemoryError, 'You ran out of memory.' | |
rescue NoMemoryError => exception_variable | |
puts 'NoMemoryError was raised', exception_variable | |
rescue RuntimeError => other_exception_variable | |
puts 'RuntimeError was raised now' | |
else | |
puts 'This runs if no exceptions were thrown at all' | |
ensure | |
puts 'This code always runs no matter what' | |
end | |
# Functions (and all blocks) implicitly return the value of the last statement | |
double(2) #=> 4 | |
# Parentheses are optional where the result is unambiguous | |
double 3 #=> 6 | |
double double 3 #=> 12 | |
# Method arguments are separated by a comma | |
sum 3, 4 #=> 7 | |
sum sum(3, 4), 5 #=> 12 | |
# yield | |
# All methods have an implicit, optional block parameter | |
# it can be called with the 'yield' keyword | |
def surround | |
puts '{' | |
yield | |
puts '}' | |
end | |
surround { puts 'hello world' } | |
# { | |
# hello world | |
# } | |
# You can pass a block to a function | |
# "&" marks a reference to a passed block | |
def guests(&block) | |
block.call 'some_argument' | |
end | |
# You can pass a list of arguments, which will be converted into an array | |
# That's what splat operator ("*") is for | |
def guests(*array) | |
array.each { |guest| puts guest } | |
end | |
# Define a class with the class keyword | |
class Human | |
# A class variable. It is shared by all instances of this class. | |
@@species = 'H. sapiens' | |
# Basic initializer | |
def initialize(name, age = 0) | |
# Assign the argument to the "name" instance variable for the instance | |
@name = name | |
# If no age given, we will fall back to the default in the arguments list. | |
@age = age | |
end | |
# Basic setter method | |
def name=(name) | |
@name = name | |
end | |
# Basic getter method | |
def name | |
@name | |
end | |
# The above functionality can be encapsulated using the attr_accessor method as follows | |
attr_accessor :name | |
# Getter/setter methods can also be created individually like this | |
attr_reader :name | |
attr_writer :name | |
# A class method uses self to distinguish from instance methods. | |
# It can only be called on the class, not an instance. | |
def self.say(msg) | |
puts msg | |
end | |
def species | |
@@species | |
end | |
end | |
# Instantiate a class | |
jim = Human.new('Jim Halpert') | |
dwight = Human.new('Dwight K. Schrute') | |
# Let's call a couple of methods | |
jim.species #=> "H. sapiens" | |
jim.name #=> "Jim Halpert" | |
jim.name = "Jim Halpert II" #=> "Jim Halpert II" | |
jim.name #=> "Jim Halpert II" | |
dwight.species #=> "H. sapiens" | |
dwight.name #=> "Dwight K. Schrute" | |
# Call the class method | |
Human.say('Hi') #=> "Hi" | |
# Variables that start with a capital letter are constants | |
Var = "I'm a constant" | |
defined? Var #=> "constant" | |
# base class | |
class Human | |
@@foo = 0 | |
def self.foo | |
@@foo | |
end | |
def self.foo=(value) | |
@@foo = value | |
end | |
end | |
# derived class | |
class Worker < Human | |
end | |
Human.foo # 0 | |
Worker.foo # 0 | |
Human.foo = 2 # 2 | |
Worker.foo # 2 | |
# Class instance variable is not shared by the class's descendants. | |
class Human | |
@bar = 0 | |
def self.bar | |
@bar | |
end | |
def self.bar=(value) | |
@bar = value | |
end | |
end | |
class Doctor < Human | |
end | |
Human.bar # 0 | |
Doctor.bar # nil | |
module ModuleExample | |
def foo | |
'foo' | |
end | |
end | |
# Including modules binds their methods to the class instances | |
# Extending modules binds their methods to the class itself | |
class Person | |
include ModuleExample | |
end | |
class Book | |
extend ModuleExample | |
end | |
Person.foo # => NoMethodError: undefined method `foo' for Person:Class | |
Person.new.foo # => 'foo' | |
Book.foo # => 'foo' | |
Book.new.foo # => NoMethodError: undefined method `foo' | |
# Callbacks are executed when including and extending a module | |
module ConcernExample | |
def self.included(base) | |
base.extend(ClassMethods) | |
base.send(:include, InstanceMethods) | |
end | |
module ClassMethods | |
def bar | |
'bar' | |
end | |
end | |
module InstanceMethods | |
def qux | |
'qux' | |
end | |
end | |
end | |
class Something | |
include ConcernExample | |
end | |
Something.bar # => 'bar' | |
Something.qux # => NoMethodError: undefined method `qux' | |
Something.new.bar # => NoMethodError: undefined method `bar' | |
Something.new.qux # => 'qux' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment