Skip to content

Instantly share code, notes, and snippets.

@pacovell
Created January 9, 2011 15:22
Show Gist options
  • Save pacovell/771749 to your computer and use it in GitHub Desktop.
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)
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