Skip to content

Instantly share code, notes, and snippets.

@JAChapmanII
Created January 29, 2013 13:09
Show Gist options
  • Save JAChapmanII/4664103 to your computer and use it in GitHub Desktop.
Save JAChapmanII/4664103 to your computer and use it in GitHub Desktop.
to_hash thing
class Thing
def initialize
@fname = ""
@lname = ""
@index = 0
end
def to_hash
hash = {}
instance_variables.each { |var|
hash[var.to_s.delete("@")] = instance_variable_get(var)
}
hash
end
attr_accessor :fname, :lname, :index
end
t = Thing.new
t.fname = "grass"
t.lname = "dirty"
t.index = 58
print t.to_hash
# outputs: {"fname"=>"grass", "lname"=>"dirty", "index"=>58}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment