-
-
Save astropanic/3833927 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
ruby x.rb | |
{"title":"SHINY NEW TITLE","bar":"blah"} |
This file contains hidden or 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
require 'json' | |
class Foo | |
attr_accessor :title, :bar | |
def initialize | |
self.title = "shiny new title" | |
self.bar = "blah" | |
end | |
def html_attributes | |
hash = {} | |
instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) } | |
hash.merge! "title" => title.upcase | |
JSON.generate(hash) | |
end | |
end | |
f = Foo.new | |
puts f.html_attributes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment