I am trying to create an object and convert it to json.
require 'json'
class Person
attr_accessor :first_name, :last_name
def to_json
hash = {}
self.instance_variables.each do |var|
hash[var] = self.instance_variable_get var
end
hash.to_json
end
end
person = Person.new
person.first_name = "Michael"
person.last_name = "Jordon"
I get the output:
person.to_json
# => {"@first_name":"Michael","@last_name":"Jordon"}
What's the change I have to make so that the @
symbol does not come as part of variable names in json string?