ruby cheat sheet code example

Example 1: ruby cheat sheet

Four good ruby cheat Sheets:
https://overapi.com/ruby
http://www.testingeducation.org/conference/wtst3_pettichord9.pdf
https://github.com/ThibaultJanBeyer/cheatsheets/blob/master/Ruby-Cheatsheet.md#basics
https://www.vikingcodeschool.com/professional-development-with-ruby/ruby-cheat-sheet

Example 2: best ruby cheat sheet

def greeting(hello, *names) # *name is a splat argument, takes several parameters passed in an array
  return "#{hello}, #{names}"
end

start = greeting("Hi", "Justin", "Maria", "Herbert") # call a method by name

def name(variable=default)
  ### The last line in here get's returned by default
end

Example 3: best ruby cheat sheet

unless false # unless checks if the statement is false (opposite to if).
puts “I’m here”
else
puts “not here”
end
# or
puts "not printed" unless true

Example 4: best ruby cheat sheet

i = 0
loop do
  i += 1
  print "I'm currently number #{i}” # a way to have ruby code in a string
  break if i > 5
end

Example 5: best ruby cheat sheet

my_array = [a,b,c,d,e]
my_array[1] # b
my_array[2..-1] # c , d , e
multi_d = [[0,1],[0,1]]
[1, 2, 3] << 4 # [1, 2, 3, 4] same as [1, 2, 3].push(4)

Example 6: best ruby cheat sheet

i = 0
until i == 6
  puts i
  i += 1
end

Tags:

Rust Example