Bfca16459c82a7836c3d5c8d79f0b640

I'm decoding a json object. The first line would be sufficient - but then I can only access the items with item["foo"]. I appreciate item[:foo]. Is there a more elegant way to do that?

1
2
3
4
5
6
7
8
9
10
11
12
def schema
  raw_schema_json = ActiveSupport::JSON.decode(self.schema_json)
  schema = []
  raw_schema_json.each do |raw_item|
    item = {}
    raw_item.each do |key, value|
      item[key.to_sym] = value
    end
    schema.push(item)
  end
  schema
end

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

June 10, 2009, June 10, 2009 18:16, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
7
def schema(json = ActiveSupport::JSON.decode(schema_json))
  case json
  when Array; json.map { |element| schema(element) }
  when Hash; HashWithIndifferentAccess.new(json)
  else; json
  end
end
511495716862032ae51c17d27d936dc1

D Clapp

June 11, 2009, June 11, 2009 17:53, permalink

No rating. Login to rate!

wow.

6cb63e2304cb64236520d0e3e353c9bc

dive.myopenid.com

June 22, 2009, June 22, 2009 19:01, permalink

No rating. Login to rate!

Adam is the man, once again :-)

Your refactoring





Format Copy from initial code

or Cancel