Skip to content

Instantly share code, notes, and snippets.

View MilanGrubnic70's full-sized avatar

Milan Grubnic MilanGrubnic70

View GitHub Profile
@MilanGrubnic70
MilanGrubnic70 / iterators.rb
Last active August 29, 2015 14:00
Iterators: loop, for, each, & times.
# An iterator is a Ruby method that repeatedly invokes a block of code.
# The 'break' keyword: Your Get Out of Jail Free card: it breaks a loop as soon as its condition is met.
# The 'Loop' method: The simplest iterator is the loop method.
loop { print "Hello, world!" } # WARNING! Infinite loop!
i = 0
@MilanGrubnic70
MilanGrubnic70 / nested_arrays.rb
Created April 19, 2014 18:33
Nested / Multidimensional Arrays
things = [[1,2,3], ["red", "blue"]] #=> create an array of arrays.
puts things[0][1] #=> Print out 2. The array with numbers is at index zero in 'things' and 2 is at index one in that sub-array.
things.each do |sub_array| #sub_array is each element of the 'things' array.
sub_array.each do |item| #The each method is called on the sub-arrays. First [1,2,3] then [1,2,3]
puts item
end
end
@MilanGrubnic70
MilanGrubnic70 / ternary_0perator.md
Last active August 29, 2015 14:00
Ternary Operator

#Ternary Operator

#####This is a shorthand statement for a simple if...else statement. It is a useful tool in situations where you have an extremely simple if...else statement where you are trying to assign a value to a variable.

boolean_expression ? true_expression : false_expression

grade = 88  
status = grade >= 70 ? "pass" : "fail"  
=> pass  

#Variables

###'Local' variables are available in the local scope

###'Block' variables are located in a code block. They are only accessable within the code block.

###'Instance' @variables begin with an @. This isn't just a Ruby convention—it's part of the syntax! Always start your instance variables with @.

###'Class' @@variables are like instance variables, but instead of belonging to an instance of a class, they belong to the class itself. Class variables always start with two @s, like so: @@files.

@MilanGrubnic70
MilanGrubnic70 / class.md
Last active August 29, 2015 14:00
Classes

Creating a class syntax:

class ClassName
    def MethodName( parameter )
        @classVariable = parameter
    end
end

class ClassName
 def initialize(param1, param2)
@MilanGrubnic70
MilanGrubnic70 / getters_and_setters.md
Last active August 29, 2015 14:00
Getter & Setter

#Getters & Setters

attr reader :variable1, :variable2, :variable3  
attr_writer :variable1, :variable2, :variable3  
attr_accessor :variable1, :variable2, :variable3

class Person  
  attr_reader :name  
  attr_writer :name  
 def initialize(name) 
@MilanGrubnic70
MilanGrubnic70 / modules_and_mixins.md
Last active August 29, 2015 14:00
Modules & Mixins

#Modules & Mixins ####CapitalizedCamelCase

You can think of a module as a toolbox that contains a set methods and constants. We keep a bunch of them in modules and only pull in those module toolboxes when we need the constants and methods inside!

You can think of modules as being very much like classes, only modules can't create instances and can't have subclasses. They're just used to store things!

module Circle
@MilanGrubnic70
MilanGrubnic70 / return.md
Last active August 29, 2015 14:00
Return

#Return

Return - All methods have a return object which will be different than the output.

Once an object is returned in a method the method breaks. Nothing below the return statement will run.

Return can only return one object. If you need to return more than one object stick them in an array which is considered one object.

#Object Relational Modeling

Class === Table
Instance === Row/Tuple
Attribute === Column/Field

@MilanGrubnic70
MilanGrubnic70 / database_relationships.md
Created April 20, 2014 13:57
Database Relationships

###One to One

###One to Many

###Many to Many