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#basicshttps://www.vikingcodeschool.com/professional-development-with-ruby/ruby-cheat-sheet
defgreeting(hello,*names)# *name is a splat argument, takes several parameters passed in an arrayreturn"#{hello}, #{names}"end
start = greeting("Hi","Justin","Maria","Herbert")# call a method by namedefname(variable=default)### The last line in here get's returned by defaultend
1.is_a?Integer# returns true:1.is_a?Symbol# returns true"1".is_a?String# returns true[1,2,3].collect!()# does something to every element (overwrites original with ! mark).map()# is the same as .collect1.2.floor # 1 # rounds a float (a number with a decimal) down to the nearest integer.
cube.call # implying that cube is a proc, call calls procs directlyTime.now # displays the actual time
array =[5,4,1,3,2]
array.sort!# = [1,2,3,4,5] – works with text and other as well."b"<=>"a"# = 1 – combined comparison operator. Returns 0 if first = second, 1 if first > second, -1 if first < second
array.sort!{|a, b| b <=> a }# to sort from Z to A instead of A to Z
gets # is the Ruby equivalent to prompt in javascript (method that gets input from the user)
gets.chomp # removes extra line created after gets (usually used like this)
"Hello".length # 5"Hello".reverse # “olleH”"Hello".upcase # “HELLO”"Hello".downcase # “hello”"hello".capitalize # “Hello”"Hello".include?"i"# equals to false because there is no i in Hello"Hello".gsub!(/e/,"o")# Hollo"1".to_i # transform string to integer –– 1"test".to_sym # converts to :test"test".intern # :test:test.to_s # converts to "test"
if1<2
puts “one smaller than two”
elsif1>2# *careful not to mistake with else if. In ruby you write elsif*
puts “elsif”
else
puts “false”
end# or
puts "be printed"iftrue
puts 3>4?"if true":"else"# else will be putted
classDerivedClass< BaseClass;end# if you want to end a Ruby statement without going to a new line, you can just type a semicolon.classDerivedClass< Base
defsome_methodsuper(optional args)# When you call super from inside a method, that tells Ruby to look in the superclass of the current class and find a method with the same name as the one from which super is called. If it finds it, Ruby will use the superclass' version of the method.# Some stuffendendend# Any given Ruby class can have only one superclass. Use mixins if you want to incorporate data or behavior from several classes into a single class.
:symbol# symbol is like an ID in html. :Symbols != "Symbols"# Symbols are often used as Hash keys or referencing method names.# They can not be changed once created. They save memory (only one copy at a given time). Faster.:test.to_s # converts to "test""test".to_sym # converts to :test"test".intern # :test# Symbols can be used like this as well:
my_hash ={key:"value",key2:"value"}# is equal to { :key => "value", :key2 => "value" }
hash ={"key1"=>"value1","key2"=>"value2"}# same as objects in JavaScript
hash ={key1:"value1",key2:"value2"}# the same hash using symbols instead of strings
my_hash =Hash.new# same as my_hash = {} – set a new key like so: pets["Stevie"] = "cat"
pets["key1"]# value1
pets["Stevie"]# cat
my_hash =Hash.new("default value")
hash.select{|key, value| value >3}# selects all keys in hash that have a value greater than 3
hash.each_key {|k| print k," "}# ==> key1 key2
hash.each_value {|v| print v }# ==> value1value2
my_hash.each_value {|v| print v," "}# ==> 1 2 3