Last active
August 29, 2015 14:07
-
-
Save lin/9edf4e4ab0d9351ebe96 to your computer and use it in GitHub Desktop.
Ruby Cheatsheet
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
#string | |
str = "str #{placeholder}" | |
str.length | |
str = str + "concatenate" | |
"albert".gsub("al", "ro") #replace | |
str[3] | |
#array | |
arr = ["alb", "ert", 25] | |
arr.size | |
arr << "lin" | |
arr.delete_at(2) | |
arr.each do { something } | |
#hash | |
hash = { al: "bert", yingkui: "lin"} | |
hash.al | |
hash.delete(:a) | |
#Ruby Bits 1 | |
unless = if ! | |
puts "albert" unless old? | |
nil == false #only these two | |
albert ||= "" # albert = "" unless albert | |
# every condition returns a value | |
albert || "Not found" | |
if object_a == object_b | |
self.method | |
private | |
def method | |
arr.from | |
arr.to | |
# module extended or included by class | |
include GameUtils # include instance methods | |
game.extend(Playable) | |
extend GameUtils # include class methods | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
extend ActiveSupport::Concern | |
included do | |
load_game_list | |
end | |
#Ruby BIts 2 | |
#Struct | |
Game = Struct.new(:name, :year, :system) do | |
def to_s | |
"#{name}#{year}" | |
end | |
end | |
#alias_method | |
alias_method :find_all, :all | |
#define_method | |
SYSTEMS = ['SNES', 'PS1', 'Genesis'] | |
SYSTEMS.each do |system| | |
define_method "runs_on_#{system.downcase}?" do | |
self.system == system | |
end | |
end | |
#send method | |
library.send(:list) | |
library.send(:emulate, "Contra") | |
library.method(:list).call | |
library.method(:emulate).call("Contra") | |
# combine | |
[:each, :map, :select].each do |method| | |
define_method method do |&block| | |
games.send(method, &block) | |
end | |
end | |
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
$rails new project_name | |
$rails server | |
$rails g scaffold name attribute:type | |
$rake db:migrate | |
$bundle install | |
$bundle check |
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
# class instance / methods | |
before_save :set_password | |
foreign_key: :product_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment