jbuilder
https://github.com/rails/jbuilder
Ruby
Jbuilder: generate JSON objects with a Builder-style DSL
Jbuilder#array!
Turns the current element into an array and iterates over the passed collection, adding each iteration as
an element of the resulting array.
Example:
json.array!(@people) do |person|
json.name person.name
json.age calculate_age(person.birthday)
end
[ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ]
If you are using Ruby 1.9+, you can use the call syntax instead of an explicit extract! call:
json.(@people) { |person| ... }
It's generally only needed to use this method for top-level arrays. If you have named arrays, you can do:
json.people(@people) do |person|
json.name person.name
json.age calculate_age(person.birthday)
end
{ "people": [ { "name": David", "age": 32 }, { "name": Jamie", "age": 31 } ] }
If you omit the block then you can set the top level array directly:
json.array! [1, 2, 3]
[1,2,3]Edit
git clone [email protected]:rails/jbuilder.git
cd jbuilder
open lib/jbuilder.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-Jbuilder-array--for-pr
# Commit to gitgit add lib/jbuilder.rbgit commit -m "better docs for Jbuilder#array!"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-Jbuilder-array--for-pr
hub pull-request
# Celebrate!