Skip to content

Instantly share code, notes, and snippets.

@namtx
Last active May 17, 2018 08:54
Show Gist options
  • Save namtx/ce736c48bdff65c784b2fc5c97e17193 to your computer and use it in GitHub Desktop.
Save namtx/ce736c48bdff65c784b2fc5c97e17193 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
class Greeting
def initialize text
@text = text
end
def welcome
@text
end
end
my_object = Greeting.new "Hello"
my_object.class # => Greeting
my_object.class.instance_methods false # => [:welcome]
my_object.instance_variables # => [:@text]
class Entity
attr_reader :table, :ident
def initialize table, ident
@table = table
@ident = ident
Database.sql "INSERT INTO #{table} (id) VALUES (#{ident})"
end
def set col, val
Database.sql "UPDATE #{table} SET #{col}='#{val}' WHERE id = #{ident}"
end
def get col
Database.sql("SELECT #{col} FROM #{table} WHERE id = #{ident}")[0][0]
end
end
def test
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment