Skip to content

Instantly share code, notes, and snippets.

@msharp
Created February 16, 2012 23:55
Show Gist options
  • Save msharp/1848921 to your computer and use it in GitHub Desktop.
Save msharp/1848921 to your computer and use it in GitHub Desktop.
Some functionas to camelize ruby hashes/objects for json output
# too much stuff to camelize the json object keys
# so that they are ready for the templates
def camelized(emp)
puts emp.inspect
if emp.is_a?(Array)
new = []
emp.each {|e| new << camelize_keys(e)}
else
new = camelize_keys(emp)
end
new
end
def camelize_keys(emp)
r = {}
emp.to_hash.each do |k,v|
r[camelize(k)] = v
end
r
end
def camelize(s)
k = s.to_s.split(//)
k.each_with_index do |char,i|
k[i+1].upcase! if char == "_"
end
k.reject{|char|char=="_"}.join
end
def de_struct(structs)
h = {}
structs.each do |str|
str.each_pair{|k,v|h[k]=v}
end
h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment