Skip to content

Instantly share code, notes, and snippets.

@laurenhavertz
Last active December 19, 2015 09:59
Show Gist options
  • Select an option

  • Save laurenhavertz/5936700 to your computer and use it in GitHub Desktop.

Select an option

Save laurenhavertz/5936700 to your computer and use it in GitHub Desktop.
Practice

####Methods to Know

  • .each

  • .map

  • .inject

  • .reject

  • .length

  • .reverse

  • .push

  • .pop

  • .shift

  • .unshift

  • .class

  • .inspect

          puts "what is your first name"
          first = gets.chomp (removes the line breaks)
          puts "you typed #{first}"
          puts first.inspect
          
          def full_name(first, last)
            return "Hello #{first} #{last}"
          end
    

                ##seperate logic from presentation/modular. Should be data and logic as opposed to outputting to the screen
            
            puts "what is your first name"
            first = gets.chomp (removes the line breaks)
            
            puts "what is your last name"
            last = gets.chomp
            
            full_name(first, last)

    a = 7
    1 = 0
    
    while ( a <= 20)
      puts a, i
      i +=1
    end

    unless (a == 3)
      puts "not 3"
    end
    
    if (a != 3)
      puts "this is also not 3"
    end

    case a
      when 5
        puts "this is 5"
      when 6 
        puts"this is 6"
      else
        puts "something different"
    end

Classes

  • LONG WAY

      class Rectangle
        attr_accessors :width, :height
      
        def inititalize (passed_in_width, passed_in_height)
          @height = passed_in_height
          @width = passed_in_width
        end
      
        def area
          @height * @width
        end
      
        #reader method
        def width
          @width
        end
      
        def height
          @height
        end
      
        #writer method 
        def width=(width)
          @width = width
        end 
      
        def height=(height)
          @height = height
        end 
      
      end
    
Short Way
    require 'pry'
    
    class Rectangle
      attr_accessors :width, :height
    
      def inititalize (passed_in_width, passed_in_height)
        @height = passed_in_height
        @width = passed_in_width
      end
    
      def area
        @height * @width
      end
    
    end
Super Classes & Inheritance
  require 'pry'

class Rectangle
  attr_accessors :width, :height

  def inititalize (passed_in_width, passed_in_height)
    @height = passed_in_height
    @width = passed_in_width
  end

  def area
    @height * @width
  end

end

class Square < Rectangle

  def initialize(side)
    super(side, side) 
  
  end

end 


a = Rectangle.new(5,4)
b = Square.new(8)

binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment