Last active
October 29, 2018 15:23
-
-
Save rodloboz/1c8c03dc7391873f62f67427e16a2817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student | |
| attr_reader :first_name, :last_name, :nationality, :gender | |
| #. first_name, last_name ... | |
| def initialize(params = {}) | |
| @first_name = params[:first_name] | |
| @last_name = params[:last_name] | |
| @nationality = params[:nationality] | |
| @gender = params[:gender] | |
| end | |
| # this is an instance method | |
| # this one is called on the instance | |
| # def categories | |
| # ["beginner", "advanced"] | |
| # end | |
| # this is a class method | |
| # need to be called on Student | |
| def self.categories | |
| # self => refers to the class | |
| puts self | |
| ["beginner", "advanced"] | |
| end | |
| # def first_name | |
| # @first_name | |
| # end | |
| # def self.class_method | |
| # self refers to the class | |
| # end | |
| def full_name | |
| # self refers to the instance | |
| # self.first_name | |
| puts self | |
| "#{@first_name.capitalize} #{@last_name.capitalize}" | |
| end | |
| def to_array | |
| [@first_name, @last_name, @nationality, @gender] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| first_name | last_name | nationality | gender | |
|---|---|---|---|---|
| ines | salgueiro | portuguese | female | |
| mathilde | bergue | french | female | |
| allenah | herholdt | south african | female | |
| antoine | welter | luxembourgish | male | |
| rui | portuguese | male |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # used for ruby library | |
| # or gems | |
| require 'csv' | |
| # require_relative | |
| # used for local files | |
| require_relative 'student' | |
| filepath = './students.csv' | |
| csv_options = { | |
| headers: :first_row, | |
| header_converters: :symbol, | |
| col_sep: ',' | |
| } | |
| # PARSING | |
| # permanent -> ephemeral | |
| # physical storage -> computer memory | |
| students = [] | |
| CSV.foreach(filepath, csv_options) do |row| | |
| # puts "#{row[:first_name].capitalize} #{row[:last_name].capitalize} is a #{row[:nationality].capitalize} #{row[:gender]}." | |
| student_hash = { | |
| first_name: row[:first_name], | |
| last_name: row[:last_name], | |
| nationality: row[:nationality], | |
| gender: row[:gender] | |
| } | |
| students << Student.new(student_hash) | |
| end | |
| p students | |
| puts "What's your name?" | |
| name = gets.chomp.downcase.split | |
| first_name = name[0] | |
| second_name = name[1] | |
| puts "What's your nationality?" | |
| nationality = gets.chomp.downcase | |
| puts "What's your gender?" | |
| gender = gets.chomp.downcase | |
| student_hash = { | |
| first_name: first_name, | |
| last_name: last_name, | |
| nationality: nationality, | |
| gender: gender | |
| } | |
| students << Student.new(student_hash) | |
| # STORING | |
| # computer memory -> physical storage | |
| CSV.open(filepath, 'wb', csv_options) do |csv| | |
| # we need to write the headers | |
| # csv << write one row at a time | |
| csv << [:first_name, :last_name, :nationality, :gender] | |
| students.each do |student| | |
| # gets executes the number of students times | |
| csv << student.to_array | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Task | |
| attr_reader :description | |
| # this one only runs once .new | |
| def initialize(description) | |
| @description = description | |
| @done = false | |
| end | |
| # def done | |
| # @done | |
| # end | |
| # return either true or false | |
| # convention uses ? | |
| def done? | |
| @done | |
| end | |
| # it changes an atribute | |
| # convention ! for destructive methods | |
| def mark_as_done! | |
| @done = true | |
| end | |
| end | |
| # ternary operator | |
| # task.done? => is either true or false? | |
| # task.done? ? "[x]" : "[ ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment