Last active
December 14, 2016 11:44
-
-
Save gabriel-dehan/a7d6f6e206761062bf172f8b7920c6a9 to your computer and use it in GitHub Desktop.
OOP in ruby Dawan
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
require_relative 'user.rb' | |
class Admin < User | |
attr_accessor :role | |
def initialize(attributes, description) | |
super(attributes) | |
@description = description | |
@admin = true | |
end | |
def full_name | |
super() + "!" | |
# @first_name + " " + @last_name + "!" | |
end | |
end | |
new_admin = Admin.new({ first_name: "Gabriel", last_name: "Dehan", age: 25 }, "Un super admin ! Je l'adore") | |
# p new_admin | |
new_admin.role = "staff" | |
# p new_admin.full_name |
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
# $ gem install awesome_print | |
require 'awesome_print' | |
module Application | |
class User | |
# attr_reader :first_name | |
# Meme chose que | |
# def first_name | |
# @first_name | |
# end | |
# attr_writer :first_name | |
# Meme chose que | |
# def first_name=(value) | |
# @first_name = value | |
# end | |
# attr_reader :first_name, :last_name, :admin | |
# attr_writer :first_name, :last_name, :admin | |
# Meme chose que | |
# attr_accessor :first_name, :last_name, :admin | |
attr_accessor :first_name, :last_name, :age | |
attr_reader :admin | |
def initialize(attributes) | |
@first_name = attributes[:first_name] || "Unknown" | |
@last_name = attributes[:last_name] || "Anonymous" | |
@age = attributes[:age] || 0 | |
@admin = attributes[:admin] || false | |
end | |
def admin? | |
@admin | |
end | |
def full_name | |
@first_name + " " + @last_name | |
end | |
def is_adult? | |
age >= 18 | |
end | |
def self.create_admin(attributes) | |
new(attributes.merge({ admin: true })) | |
end | |
def self.get_all | |
return DB | |
end | |
def self.find(desired_name) | |
return DB.select { |current_user| current_user.first_name == desired_name } | |
end | |
end | |
end | |
gaby = Appplication::User.new({ age: 25, first_name: "Gabriel", last_name: "Dehan" }) | |
gab = Appplication::User.new({ age: 27, first_name: "Gabriel", last_name: "Lancel" }) | |
mel = Appplication::User.new({ age: 32, first_name: "Melanie", last_name: "Dutemps" }) | |
dominique = Appplication::User.new({ age: 16, first_name: "Dominique", last_name: "Dutemps" }) | |
admin = Appplication::User.create_admin({ age: 16, first_name: "Dominique", last_name: "Dutemps" }) | |
puts gaby | |
DB = [] | |
DB.push(gaby) | |
DB.push(gab) | |
DB.push(mel) | |
DB.push(dominique) | |
gabriels = Appplication::User.find("gabriel") | |
user_list = Appplication::User.get_all | |
ap gabriels | |
=begin | |
begin | |
Appplication::User.new("Gabriel", "Dehan", 3) | |
# ... | |
rescue Exception => e | |
raise ZeroDivisionError.new("Il y a eu une erreur") | |
rescue ZeroDivisionError => e | |
puts "Vous avez essaye de diviser par zero !" | |
end | |
=end | |
# Appplication::User.create_fake_user | |
# user = Appplication::User.new # => Methode de classe | |
# user.set_admin(true) # => Methode d'instance |
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
require 'awesome_print' | |
class User | |
attr_accessor :first_name, :last_name, :age | |
attr_reader :admin | |
def initialize(attributes) | |
@first_name = attributes[:first_name] | |
@last_name = attributes[:last_name] | |
@age = attributes[:age] | |
@admin = attributes[:admin] || false | |
end | |
def full_name | |
@first_name + " " + @last_name | |
end | |
def to_s | |
"Hello je suis #{@first_name}" | |
end | |
end | |
new_user = User.new({ first_name: "Gabriel", last_name: "Dehan", age: 25 }) | |
puts new_user | |
# puts new_user.inspect | |
puts new_user # => puts new_user.to_s | |
p new_user # => puts new_user.inspect | |
# new_user.set_admin | |
# PAS POSSIBLE ! : new_user.set_admin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment