Ruby
This document covers Strings, Hashes, Arrays, Loops, and Conditionals in Ruby.
'happy'.object_id
'happy'.gsub('ha', 'HAHAHA')
'happy'.split('')
'happy'.chars()
lichy = 'Lichard DeGray'
"My name is not #{lichy}" # String interpolation
number = 99
"#{ number } bottle#{ 's' unless number==1 } of beer on the wall... #{ number } bottle#{ 's' unless number==1 } of beer... take one down... pass it around... #{ number-1 } bottles of beer on the wall"
nums = Array(1..2) # auto-populate
letters = Array('a'..'z') # auto-populate
my_things = ['apartment', 'laptop', 'cat', 'wii u'] # manual declaration
my_things.sample
my_things.at(1)
my_things.first
my_things.last
arr = [1, 2, 3, 4]
arr.push(5) #=> [1, 2, 3, 4, 5]
arr << 6 #=> [1, 2, 3, 4, 5, 6]
arr = [1, 2, 3, 4, 5, 6]
arr.pop #=> 6
arr #=> [1, 2, 3, 4, 5]
arr.shift #=> 1
arr #=> [2, 3, 4, 5]
arr.delete_at(2) #=> 4
arr #=> [2, 3, 5]
lich = {:name => 'Lichard', :age => 3}
kat = {:name => 'Kathew', :age => 3}
om = {:name => 'Omily', :age => 3}
sample_hash = {'one'=>1,'two'=>2}
sample_hash_2 = {:one=>1, :two=>2}
sample_hash_3 = {one:1,two:2}
sample_hash_4 = {'one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5}
sample_hash_4["one"] #=> Returns 1
sample_hash_4["five"] #=> Returns 5
sample_hash_4['one'] = 10
n = 1
while n < 11
puts n
n += 1
end
n = 1
until n > 10
puts n
n += 1
end
sample_array = Array(1..5)
sample_array.each{|elem| puts elem}
sample_array.each do |elem|
puts elem
puts "The long way!!!"
end
each_example = [2,3,4].each{|elem| elem ** 2}
map_example = [2,3,4].map{|elem| elem ** 2}
p each_example
p map_example
each_example_2 = {two:2,three:3,four:4}.each{|key,value| elem ** 2}
map_example_2 = {two:2,three:3,four:4}.map{|key,value| elem ** 2}
p each_example_2
p map_example_2
one-liners
If/Elsif/Else
Unless you're using a one-liner then you need to end your if statement with and 'end' statement.
x = 2
if x < 3
puts 'less than 3'
end
* Writing the same thing as a one-liner
if x < 3 then puts 'less than 3' end
puts 'less than 3' if x < 3
* Using elsif
if x < 2
puts "less than 2"
elsif x == 2
puts "It's two!"
else
puts 'greater than two!'
end
x = true
puts "it's true!" if x != false
unless x == false
puts "it's true!"
end
* Unless can also be used as a one-liner
puts "it's true!" unless x == false
*** You cannot use elsif with unless, only else ***
unless x == false
puts "it's true!"
else
puts "it's false!"
end
unless x == false
puts "it's true!"
elsif
puts "it's false!"
end
#returns an error