Created
January 9, 2011 15:22
-
-
Save pacovell/771749 to your computer and use it in GitHub Desktop.
Simplest way to generate an unquoted Javascript option (for example, for a function)
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 JavascriptUnquoted | |
def initialize(text) | |
@text = text | |
end | |
def to_javascript | |
@text | |
end | |
end | |
class Hash | |
def to_javascript | |
script = self.inject([]) do |items, (k,v)| | |
pair = [k.to_json] | |
if v.respond_to?(:to_javascript) | |
pair << v.to_javascript | |
else | |
pair << v.to_json | |
end | |
items << pair.join(':') | |
end | |
"{#{script.join(",")}}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment