Skip to content

Instantly share code, notes, and snippets.

@leahgarrett
Last active March 26, 2019 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leahgarrett/bd72723df983169498f8c222145615e3 to your computer and use it in GitHub Desktop.
Save leahgarrett/bd72723df983169498f8c222145615e3 to your computer and use it in GitHub Desktop.
Rails Prep Intro

Rails prep

  • Two weeks until the end of term

  • Next term Rails and the Rails project


What we will be covering

  • Best practices
  • Testing
  • Working with larger projects
  • More on objects
  • Databases
  • MVC pattern
  • Sinatra

Scaling up

  • larger code bases
    • splitting code into multiple files
    • organising files
    • recognising and implementing patterns
  • processes
    • more git practice

Automate All the Things

Automate all the Things

  • Lazy developer

  • ROI

    • how much time on automation vs time on project
  • Rubocop

    • Coding Style for Rails - best practices for methods lengths, examples

Review Objects

  • Review a class and the various variable types and scope
    • variable local to a method
    • instance variable
    • class variable
class Person

    @@employee_number = 0
    def initialize(name, job_title, phone, salary)
        @name = name
        @job_title = job_title
        @phone = phone
        @salary = salary
        @@employee_number = @@employee_number + 1
        @number = @@employee_number
    end

    # getters
    def name
        return @name
    end

    def job_title
        return @job_title
    end

    def phone
        return @phone
    end

    def salary
        return @salary
    end

    def description
        return "#{@name} is a #{job_title}"
    end
end

kerry = Person.new("Kerry MacFarlane", "developer", "555-1234", "60000")
terry = Person.new("Terry Grayson", "manager", "555-3456", "70000")
sam = Person.new("Sam Ferguson", "developer", "555-4321", "65000")

puts kerry.description
puts terry.description
puts sam.description
class Person
@@employee_number = 0
def initialize(name, job_title, phone, salary)
@name = name
@job_title = job_title
@phone = phone
@salary = salary
@@employee_number = @@employee_number + 1
@number = @@employee_number
end
# getters
def name
return @name
end
def job_title
return @job_title
end
def phone
return @phone
end
def salary
return @salary
end
def description
return "#{@name} is a #{job_title}"
end
end
kerry = Person.new("Kerry MacFarlane", "developer", "555-1234", "60000")
terry = Person.new("Terry Grayson", "manager", "555-3456", "70000")
sam = Person.new("Sam Ferguson", "developer", "555-4321", "65000")
puts kerry.description
puts terry.description
puts sam.description
# 0. Run and verify it works
# 1. Use rubocop to detect violations
# 2. Fix all the rubocop violations
# 3. Run and verify the examples still work
# 4. Update the initialize to sentence case the job titles
# 5. Add another person example with a multi-word job title to test this
# 6. Change the description method to include the salary. Use money formatting.
# 7. Change the class to use an attribute accessor and delete the four getters
# 8. Run and verify the examples still work
# 9. Create a getter for the class variable employee number
# 10. Write code to display the value for employee number
# class to demo ruby features
class Person
attr_reader :name, :job_title, :phone, :salary
@employee_number = 0
def initialize(name, job_title, phone, salary)
@name = name
@job_title = job_title
@phone = phone
@salary = salary.to_i
@number = Person.increase_number
end
def self.increase_number
@employee_number += 1
end
def description
"#{@name} is a #{@job_title} and has salary #{@salary} ##{@number}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment