Logo

Ruby Cheat Sheet (In progress…)

Show more Hide more
Print

Arrays

Array Create

createarr = [1, 'two', 2.0] Documentation
newarr = Array.new; Array.new(3) Documentation

Array Access

indexarr[2], arr[-1] # 2.0 Documentation
start, countarr[1, 2] # ['two',2.0] Documentation
rangearr[1..2] # ['two',2.0] Documentation
atarr.at(2) # 2.0 Documentation
firstarr.first # 1 Documentation
lastarr.last # 2.0 Documentation
takearr.take(2) # '1,'two' (First n elements) Documentation
droparr.drop(2) # 2.0 (Opposite of take) Documentation

Array Info

lengtharr.length # 3 Documentation
countarr.count # 3 Documentation
empty?arr.empty? # false Documentation
include?arr.include?('Dig') # false Documentation

Array Modify

# Adding items
pusharr.push('Dig') # [1,'two',2.0,'Dig'] Documentation
<< operatorarr << 'Dig' # [1,'two',2.0,'Dig'] Documentation
unshiftarr.unshift(0) # [0,1,'two',2.0] Documentation
insertarr.insert(2, 'Dig') # [1,'two','Dig',2.0] Documentation
# Removing items
poparr.pop # 2.0 (Removes/returns last item) Documentation
shiftarr.shift # 1 (Removes/returns first item) Documentation
delete_atarr.delete_at(1) # [1,2.0] Documentation
deletearr.delete('two') # [1,2.0] Documentation
compact (compact!)arr.compact # Removes nil values Documentation
uniqarr.uniq # Remove duplicate elements Documentation

Array Iterate

eacharr.each {|e| p "* #{e}"} # * 1 * two * 2.0 Documentation
reverse_eacharr.reverse_each {|e| p e } # 2.0 two 1 Documentation
maparr=[1,2,3]; arr.map {|e| 2*e } # [2,4,6] Documentation
inject(1..4).inject(:+) # 10 Documentation

Array Select

arr2 = [1,2,3,4,5,6]
selectarr2.select {|e| e > 3} # [4,5,6] Documentation
rejectarr2.reject {|e| e > 3} # [1,2,3] Documentation
drop_whilearr2.drop_while {|e| e < 4} # [4,5,6] Documentation
delete_if *arr2.delete_if {|e| e < 4} # [4,5,6] Documentation
keep_if *arr2.keep_if {|e| e < 4} # [1,2,3] Documentation
* destructive

Hashes

Hash Create

create with symbolsh = {a: 'one', b: 2} Documentation
create => (hash rocket)h = {:a => 'one', :b => 1} Documentation

Hash Access

[:symbol]h[:a] # one Documentation

Strings

String Info

str = 'Dig It'
lengthstr.length # 6 Documentation
countstr.count('i') # 2 Documentation
include?str.include?('ig') # true Documentation

String Modify

capitalizestr.capitalize # Dig it Documentation
upcasestr.upcase # DIG IT Documentation
downcasestr.downcase # dig it Documentation
strip' Dig it '.strip # 'Dig it' Documentation
lstrip' Dig it '.lstrip # 'Dig it ' Documentation
rstrip' Dig it '.rstrip # ' Dig it' Documentation
chomp'Dig it\n\n'.chomp # 'Dig it' Documentation
deletestr.delete("i") # 'Dg It' Documentation
substr.sub(/[iI]/, '*') # 'D*g It' Documentation
gsubstr.gsub(/[iI]/, '*') # 'D*g *t' Documentation
slicestr.slice(3..5) # 'g It' Documentation
splitstr.split(' ') # ['Dig','It'] Documentation
trstr.tr('ig', 'og') # 'Dog It' Documentation

String Convert

to_sym'dig'.to_sym # :dig Documentation

Numbers & Math

Float

f = 12345.6789
roundf.round(1) # 12345.7 Documentation
floorf.floor(1) # 12345.6 Documentation
ceilf.ceil(1) # 12345.7 Documentation
truncatef.truncate(1) # 12345.6 Documentation
to_i1.2.to_i # 1 Documentation
modulo10.0 % 3 # 1.0 Documentation
divmod11.0.divmod(4) # [2,3.0] Documentation
eql?2.0.eql?(2.0) # true Documentation
rationalize 0.3.rationalize # (3/10) Documentation

Ranges

Specify range

Include end value(1..4).to_a # [1,2,3,4] Documentation
Exclude end value(1...4).to_a # [1,2,3] Documentation

Dates & Times

Date

todayDate.today # 2023-06-07 Documentation
newDate.new(2023,6,7) # 2023-06-07 Documentation
parseDate.parse('2023-06-07') # 2023-06-07 Documentation
strptimeDate.strptime('2001-02-03', '%Y-%m-%d') # 2001-02-03 Documentation

DateTime

newDateTime.new(2023,6,7,4,5,6) # 2023-06-07T04:05:06+00:00 Documentation
strptimeDateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') # 2001-02-03T04:05:06 Documentation

Control structures

if … elsif … else … end Documentation
unless … end Documentation
case … when … else … end Documentation
while … end Documentation
until … end Documentation
do |v| break if true … Documentation
do |v| next if true … Documentation
do |v| redo if true … Documentation

Blocks

defdef something ⮐ … ⮐ end Documentation
eacharr.each { |v| puts v } Documentation

Comments

# Comment # This is an example comment Documentation