Skip to content

Instantly share code, notes, and snippets.

@madeindjs
Last active January 8, 2019 09:53
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 madeindjs/5cad098ef558f528aa54985e79258ed4 to your computer and use it in GitHub Desktop.
Save madeindjs/5cad098ef558f528aa54985e79258ed4 to your computer and use it in GitHub Desktop.
A simple of OOP in ruby
# 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