parm530
3/13/2018 - 3:27 PM

Ruby Basics

Refreshers, shortcuts of Ruby ...

Ruby

  • Object Oriented Programming
  • Everything in Ruby is an object!

Variables

  • defined: name = 'Parm'
  • EXCEPTION: VARIABLES ARE NOT OBJECTS!

Object Declarations

Integers

  • num = 1

Strings

  • name = "Parm"

String interpolation

  • Used to evaluate a variable inside of double quotes
  • "#{var_name}"

String multiplication

  • "hi"*5 = "hihihihihi"

Arrays

  • Declaration: myArray = []
  • Contains a collection of ordered objects (doesn't have to be the same type)

Retreive

  • myArray[1]

Set

  • myArray[1] = "hi

Insert

  • myArray << "bye"

Clear

  • myArray.clear OR
  • myArray = []

Hashes

  • myHash = {}
  • unordered collection
  • key/value pairs are used to retrieve data from a hash

Retreive

  • myHash["key1"]

Set

  • myHash["key1"] = "val1"

Symbols

  • label used to identify a piece of information
  • Declaration: :test
  • Symbols occupy the same memory address!

Boolean

  • true OR false
  • Used in comparisons

Range

  • Inclusive Range: 1..10, nuumbers from 1 - 10, including 10
  • Exclusive Range: 1...10, numbers from 1 to 9, not including 10

Converting a Range to Array

  • (1..10).to_a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • Works for characters also: alpha = 'a'..'m', alpha.include?('b') -> true

Constant

  • a variable that stays the same in value over time
  • Declaration: TEST = 1, all caps

Control Flow

if/elsif/else

if condition

else 

end

# can also be written as:
condition ? true (result) : false (result)

# Example
(5 < 4) ? puts "4 is greater than 5" : puts "No, 4 is not greater than 5"


# Multiple conditions
if condition1

elsif condition2

end

While

while condition do

end

For

# array could also be a range 0..5
for variable in array do

end

Unless

# unless is equivalent to if !(true)
unless (condition is false)

end

Case

case test_value
when value
  ....
when value2
  ...
when value3 
  ....
else

end

Methods

# create a method

def name
  #...
end

# create method with args

def name_of_method(arg1)
  #...
end

Classes

OBJECT ORIENTED PROGRAMMING

# defining a class
class NameOfClass     #camelcased and 1st letter is capitalized

  # Class methods should be defined above instance methods
  def self.classMethod1
    ...
  end
  
  
  # Instance class
  def method1
    ...
  end
  
end


# create an instance 
obj1 = NameOfClass.new

# call on the instance methods:
obj.method1

#call on a Class Method:
NameOfClass.classMethod1
  • Attributes are basically variables that contain information about the instance
    • Scoped with @ and are called instance variables
    • Ex: @name
    • YOU NEVER EVER HAVE ACCESS TO THE INSTANCE VARIABLE OUTSIDE OF THE CLASS!!
  • You can access instance methods using getter and setter methods!
#still inside of class declaration
#setter method
def name=(name)
  @name = name
end

#getter method
def name
  @name
end

# Outside of class:
parm = Person.new
parm.name = "Parm" #setting the name
parm.name #returns "Parm"

  • For each attribute you will need a getter and setter method, to be able to retrieve and set the value of the attribute!
  • Rails provides a shortcut: attr_accessor :name
    • This is a method that returns both the getter and setter method!
  • Rails also provides attr_reader and attr_writer in case you just want a method to only set or only retrieve an attributes value!

initialize Method

  • used to set attribute values for a newly created object
  • called after ClassName.new
#inside of a class
def initialize(arg1, arg2, arg3)
  @arg1 = arg1
  @arg2 = arg2
  ...
end


Interesting functions

Function name []

def [](arg) 

end
  • Nothing related to hashes or arrays
  • Syntactic vinegar for applying a method on a variable
  • Pay attention to the class that it inherits from (could be related to array or hash class)
  • Example of how it work:
class Parm
  def []
    "hello"
  end
end

parm = Parm.new
parm[] # => "hello"
#same as 
parm.[] # => "hello"

class << self

  • Once you see this within a Rails class, its an alternative of saying: the following methods defined in this sub class are CLASS METHODS

class Parm
  class << self
    def say_hi
      "HI"
    end
  end
  
  #SAME AS:
  
  def self.say_hi
    "HI"
  end
  
  def say_bye
    "BYE"
  end
end

# method say_hi can only be called by saying:
Parm.say_hi

#while method say_bye is called by an instance of that class:
Parm.new.say_bye