statements

unless

the opposite of an if/else statement. like natural language: unless this statement is false, then execute the following block.

name = "kat"
 
unless name.empty?
	puts name
end
# returns "kat" unless name is undefined

loops

foreach with each

  • repeat until done
  • iterates/executes on each element in the array
swears = ["fuck", "shit", "lol"]
 
swears.each do |swear| # a is a variable that represents one element in the array at a time
	puts "oh #{swears}"
end
# oh fuck
# oh shit
# oh lol

operators

defined?

checks if a variable/method/expression is defined and returns its type

nmb = 12
 
puts defined?(nmb) # puts nmb.defined? does not work!
# local-variable
 
def fuck
	puts "you"
end
 
puts defined?(fuck)
# method

nil?, empty? & blank?

nil? can be used on anything. returns true if object/thing it is used on is nil

empty? can be used on strings, arrays, and hashes. only returns true if length of one of those is 0

blank? is like empty? except it won’t crap out (throw NoMethodError) if the string/array/has is nil instead of 0

methods

puts

print something (adds a newline). it’s like echo kinda but with a newline

mytext = "fuck"
puts mytext
# fuck

puts vs print vs p

puts adds a newline, print doesn’t

p displays the raw object, meaning it shows when a string is a string by adding quotes around it, a number as an integer, and an array with brackets around it. syntax highlighting makes this easier to discern

chomp

removes newlines (\n)

usage:

mytext = "hello\n"
puts mytext.chomp
# hello