Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

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
Comment

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
Comment

best ruby cheat sheet

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 .collect
1.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 directly
Time.now # displays the actual time
Comment

best ruby cheat sheet

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
Comment

best ruby cheat sheet

10.upto(15) { |x| print x, " " } # 10 11 12 13 14 15
"a".upto("c") { |x| print x, " " } # a b c
Comment

best ruby cheat sheet

hashes.each do |x,y|
  print "#{x}: #{y}"
end
Comment

best ruby cheat sheet

things.each do |item| # for each things in things do something while storing that things in the variable item
  print “#{item}"
end
Comment

best ruby cheat sheet

10.times do
  print “this text will appear 10 times”
end
Comment

best ruby cheat sheet

for i in 1..5
  next if i % 2 == 0 # If the remainder of i / 2 is zero, we go to the next iteration of the loop.
  print i
end
Comment

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
Comment

best ruby cheat sheet

for i in 1...10 # ... tells ruby to exclude the last number (here 10 if we .. only then it includes the last num)
  puts i
end
Comment

best ruby cheat sheet

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

best ruby cheat sheet

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)
Comment

best ruby cheat sheet

i = 1
while i < 11
  puts i
  i = i + 1
end
Comment

best ruby cheat sheet

"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"
Comment

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
Comment

best ruby cheat sheet

if 1 < 2
puts “one smaller than two”
elsif 1 > 2 # *careful not to mistake with else if. In ruby you write elsif*
puts “elsif”
else
puts “false”
end
# or
puts "be printed" if true
puts 3 > 4 ? "if true" : "else" # else will be putted
Comment

best ruby cheat sheet

# single line comment
Comment

best ruby cheat sheet

=begin
Bla
Multyline comment
=end
Comment

best ruby cheat sheet

lambda { |param| block }
multiply = lambda { |x| x * 3 }
y = [1, 2].collect(&multiply) # 3 , 6
Comment

best ruby cheat sheet

class DerivedClass < BaseClass; end # if you want to end a Ruby statement without going to a new line, you can just type a semicolon.

class DerivedClass < Base
  def some_method
    super(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 stuff
    end
  end
end

# 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.
Comment

best ruby cheat sheet

: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" }
Comment

best ruby cheat sheet

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
Comment

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)
Comment

best ruby cheat sheet

MY_CONSTANT = # something
Comment

best ruby cheat sheet

"bla,bla".split(“,”) # takes sting and returns an array (here  ["bla","bla"])
Comment

PREVIOUS NEXT
Code Example
Ruby :: add key and value to first spot in hash ruby 
Ruby :: how to open ruby console 
Ruby :: rails include dynamic 
Ruby :: sudo text logs 
Ruby :: irb loaderror 
Ruby :: difference between is_a and kind_of ruby 
Ruby :: ruby classes 
Ruby :: singning in using username rails 
Ruby :: rails active record to fetch only list of ids 
Ruby :: paranoia gem 
Ruby :: ruby extract elements from array 
Ruby :: ruby on rails project 
R :: update r from rstudio 
R :: ggplot increase label font size 
R :: knnImputation in r 
R :: r stack data frames 
R :: create file in r 
R :: how to read file in r 
R :: how to create dictionary in R 
R :: how to read csv file in r 
R :: how to use ifelse in r 
R :: calculating RMSE, MAE, MSE, Rsquared manually in R 
R :: reduce ggtitle size 
R :: convert int to character R 
R :: R Basic Syntax 
R :: find the number of times a variable is repeated in a vector r 
R :: pairwise combinations r 
R :: how to filter in R whitout lossing NA values 
R :: take node names from nodes to edges tidygraph 
R :: r alluvial chart with NA 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =