Last active
May 17, 2018 08:54
-
-
Save namtx/ce736c48bdff65c784b2fc5c97e17193 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
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
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] |
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
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 |
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
def test | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment