Created
June 20, 2016 18:49
-
-
Save idimitrov07/f67956e4127214b668e99a879c3809f3 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 Computer | |
| $manufacturer = "Mango Computer, Inc." | |
| @@files = {hello: "Hello, world!"} | |
| def initialize(username, password) | |
| @username = username | |
| @password = password | |
| end | |
| def current_user | |
| @username | |
| end | |
| def self.display_files | |
| @@files | |
| end | |
| end | |
| # Make a new Computer instance: | |
| hal = Computer.new("Dave", 12345) | |
| puts "Current user: #{hal.current_user}" | |
| # @username belongs to the hal instance. | |
| puts "Manufacturer: #{$manufacturer}" | |
| # $manufacturer is global! We can get it directly. | |
| puts "Files: #{Computer.display_files}" | |
| # @@files belongs to the Computer class. | |
| ################ | |
| class MyClass | |
| $my_variable = "Hello!" | |
| end | |
| puts $my_variable | |
| ########### | |
| class Person | |
| # Set your class variable to 0 on line 3 | |
| @@people_count = 0 | |
| def initialize(name) | |
| @name = name | |
| # Increment your class variable on line 8 | |
| @@people_count += 1 | |
| end | |
| def self.number_of_instances | |
| # Return your class variable on line 13 | |
| @@people_count | |
| end | |
| end | |
| matz = Person.new("Yukihiro") | |
| dhh = Person.new("David") | |
| puts "Number of Person instances: #{Person.number_of_instances}" | |
| ################# | |
| class ApplicationError | |
| def display_error | |
| puts "Error! Error!" | |
| end | |
| end | |
| class SuperBadError < ApplicationError | |
| end | |
| err = SuperBadError.new | |
| err.display_error | |
| ############### | |
| class Creature | |
| def initialize(name) | |
| @name = name | |
| end | |
| def fight | |
| return "Punch to the chops!" | |
| end | |
| end | |
| # Add your code below! | |
| class Dragon < Creature | |
| def fight | |
| return "Breathes fire!" | |
| end | |
| end | |
| ######################## | |
| class Creature | |
| def initialize(name) | |
| @name = name | |
| end | |
| def fight | |
| return "Punch to the chops!" | |
| end | |
| end | |
| # Add your code below! | |
| class Dragon < Creature | |
| def fight | |
| puts "Instead of breathing fire..." | |
| super | |
| end | |
| end | |
| d = Dragon.new("dragiii") | |
| d.fight | |
| ############ | |
| class Message | |
| @@messages_sent = 0 | |
| def initialize(from, to) | |
| @from = from | |
| @to = to | |
| @@messages_sent += 1 | |
| end | |
| end | |
| class Email < Message | |
| def initialize(from, to) | |
| super | |
| end | |
| end | |
| my_message = Message.new("me", "her") | |
| ################ | |
| class Computer | |
| @@users = {} | |
| def initialize(username, password) | |
| @username = username | |
| @password = password | |
| @files = {} | |
| @@users[username] = password | |
| end | |
| def create(filename) | |
| time = Time.now | |
| @files[filename] = time | |
| puts "New file created: #{filename} - #{time} - #{@username}" | |
| end | |
| def update(filename) | |
| time = Time.now | |
| @files[filename] = time if @files[filename] | |
| puts "File #{filename} updated!" | |
| end | |
| def Computer.get_users | |
| @@users | |
| end | |
| end | |
| my_computer = Computer.new("john", "1234") | |
| my_computer.create("new_file.pdf") | |
| puts Computer.get_users | |
| sleep(4) | |
| my_computer.update("new_file.pdf") | |
| puts Computer.get_users | |
| my_computer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment