Basics
@object.methods # returns a list of all methods available on this object
# "and" and "&&" are the same
thing1 and thing2
thing1 && thing2
# "or" and "||" are the same
thing1 or thing2
thing1 || thing2
# Loop a certain number of times with #times
5.times do
puts "Hello, world!"
end
#=> Alternative fact number 0
#=> Alternative fact number 1
#=> Alternative fact number 2
#=> Alternative fact number 3
#=> Alternative fact number 4Object equality
#eql? checks both the value type and the actual value it holds.
5.eql?(5.0) #=> false; although they are the same value, one is an integer and the other is a float
5.eql?(5) #=> true
#equal? checks whether both values are the exact same object in memory.
a = 5
b = 5
a.equal?(b) #=> trueArrays
# Adding arrays concatenates
a = [1, 2, 3]
b = [3, 4, 5]
a + b #=> [1, 2, 3, 3, 4, 5]
a.concat(b) #=> [1, 2, 3, 3, 4, 5]
# Subtracting arrays returns a copy of the first array,
# removing any values from the second array
[1, 1, 1, 2, 2, 3, 4] - [1, 4] #=> [2, 2, 3]Hashes
A hash is like a dictionary where the keys and values can be different.
# Any old key and value type
hash = { 9 => "nine", :six => 6 }
# Accessing values in a hash
shoes = {
"summer" => "sandals",
"winter" => "boots"
}
shoes["summer"] #=> "sandals"
# Adding/changing values
shoes["fall"] = "sneakers"
shoes #=> {"summer"=>"sandals", "winter"=>"boots", "fall"=>"sneakers"}
shoes["summer"] = "flip-flops"
shoes #=> {"summer"=>"flip-flops", "winter"=>"boots", "fall"=>"sneakers"}
# Removing values
shoes.delete("summer") #=> "flip-flops"
shoes #=> {"winter"=>"boots", "fall"=>"sneakers"}
# Merging hashes
hash1 = { "a" => 100, "b" => 200 }
hash2 = { "b" => 254, "c" => 300 }
# Duplicated keys will result in the value from the hash on the right remaining
hash1.merge(hash2) #=> { "a" => 100, "b" => 254, "c" => 300 }
# Symbols as hash keys
# 'Rocket' syntax
american_cars = {
:chevrolet => "Corvette",
:ford => "Mustang",
:dodge => "Ram"
}
# 'Symbols' syntax
japanese_cars = {
honda: "Accord",
toyota: "Corolla",
nissan: "Altima"
}
# Accessing a hash with symbol keys
american_cars[:ford] #=> "Mustang"
japanese_cars[:honda] #=> "Accord"Ruby QuickRef | zenspider.com | by ryan davis
https://stackoverflow.com/questions/10173097/rails-rspec-difference-between-let-and-let
Local debugging
bundle exec rails console opens a REPL
Then we can do stuff like
${entityName}.all to run a select * from ${entityName}
${entityName}.count to run a select COUNT(*) from ${entityName}