1

我想实现这样的目标:

  def to_indexed_json
    { 
      if source.respond_to?(:app_id)
        app_id: source.app_id
      end
      id: self.id,
      content: self.content
    }.to_json
  end
4

2 回答 2

2
def to_indexed_json
  result = {id: self.id, content: self.content}
  result.merge!(app_id: source.app_id) if source.respond_to?(:app_id)
  result.to_json
end
于 2013-01-28T16:54:10.537 回答
0
data = Hash.new
if source.respond_to?(:app_id)
  data['app_id']: source.app_id
end
data['id']: self.id,
data['content']: self.content
data.to_json
于 2013-01-28T16:14:27.217 回答