百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

dish

> 编程语言
开源

将哈希转换为普通 Ruby 对象非常简单。在 RubyMotion 中也可使用。

287 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

将哈希转换为普通 Ruby 对象非常简单。在 RubyMotion 中也可使用。

Dish!

Very simple conversion of hashes to plain Ruby objects. This is great for consuming JSON API's which is what I use it for. Also works in RubyMotion.

Installation

Add this line to your application's Gemfile:

gem "dish"

Then run:

$ bundle

Or install it yourself:

$ gem install dish

If you want a to_dish helper method added to your Hash and Array objects, you can require dish/ext in your Gemfile:

gem "dish", require: "dish/ext"

Installation in RubyMotion

Dish fully supports RubyMotion, enabling you to easily consume JSON API's in your Ruby iOS apps.

For installation in RubyMotion, add this line to your project's Gemfile:

gem "dish", require: "dish/motion"

Then run:

$ bundle

And you're good to go.

NOTE: If you're using Dish with the BubbleWrap JSON module, please see below.

Example

ruby
hash = {
  title: "My Title",
  authors: [
    { id: 1, name: "Mike Anderson" },
    { id: 2, name: "Well D." }
  ],
  active: false
}

book = Dish(hash) # or hash.to_dish if you required "dish/ext"
book.title           # => "My Title"
book.authors.length  # => 2
book.authors[1].name # => "Well D."
book.title?          # => true
book.active?         # => false
book.other           # => nil
book.other?          # => false

Coercion

Values can automatically be coerced, for example into a custom Dish object or a Time, for example if you have an updated_at in the source.

…

This is inspired by Hashie's coercion methods.

Have fun!

Converting back to Ruby/JSON objects

You can use the Dish::Plate#to_h method for accessing the original hash. In addition Dish::Plate#to_json can be used for marshaling JSON if you are using RubyMotion (NSJSONSerialization is used) or have required the "json" Ruby stdlib.

NOTE: Previously Dish::Plate#to_h was called Dish::Plate#as_hash. The Dish::Plate#as_hash method is now deprecated.

Notes

Using with the BubbleWrap JSON module

When you use the BubbleWrap gem to parse JSON into a hash, you can't use the Hash#to_dish methods directly because the BW::JSON module returns some sort of hash that hasn't got the methods from the real hash. I'm fixing this, but in the meanwhile you can achieve the same result by doing this:

ruby
BW::HTTP.get("http://path.to/api/books/2") do |response|
  json = BW::JSON.parse(response.body.to_s)
  book = Dish(json) # This is the actual conversion

  title_label.text = book.title
  author_label.text = book.authors.map(&:name).join(", ")
end

Contributing

Feature additions are very welcome, but please submit an issue first, so we can discuss it in advance. Thanks.

  1. Fork the project
  2. Create a feature branch (git checkout -b my-new-feature)
  3. Make your changes, including tests so it doesn't break in the future
  4. Commit your changes (git commit -am 'Add feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new pull request

Issues· 5 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言