Last active
January 8, 2019 09:53
-
-
Save madeindjs/5cad098ef558f528aa54985e79258ed4 to your computer and use it in GitHub Desktop.
A simple of OOP in ruby
This file contains 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
# A class who provide access to database | |
class Record | |
attr_accessor :title | |
# Create and return the record | |
def self.create(title) | |
record = Record.new | |
record.title = title | |
record.save | |
record | |
end | |
# save the record into the database | |
def save | |
puts "INSERT INTO question (title) VALUES ('#{@title}')" | |
end | |
# destroy record form database | |
def destroy | |
puts "DELETE FROM question WHERE title = '#{@title}'" | |
end | |
end | |
# A record with a title | |
class Question < Record | |
end | |
my_question = Question.create 'How are you?' | |
# => "INSERT INTO question ..." | |
my_question.destroy | |
# => "DELETE FROM question ..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment