开发者

in ruby, how do you make this nested hash work?

开发者 https://www.devze.com 2022-12-28 15:39 出处:网络
this one creates an error: @settings = { :tab1 => { :name => { :required => true }, :description }

this one creates an error:

  @settings = { 
    :tab1 => {
      :name => {
        :required => true
      },
      :description
   }
  }

need to change :descrpition to :description => {}, but i don't have any values开发者_开发知识库 for :description so i want it to remain as is (without the empty => {})

Would you show me the best way to handle this kind of situation?

thanks in advance


You can assign nil to it.

  @settings = { 
    :tab1 => {
      :name => {
        :required => true
      },
      :description => nil
   }
  }


Ruby's Hash prior to 1.9 is not ordered, and even afterwards it's a bit clumsy, as AFAIK you can't reorder items etc., so if you also want to preserve the order of the elements, you may consider using array instead of hash:

@settings = {
  :tab1 => [
    {
      :field => :name,
      :required => true
    },
    {
      :field => :description
    }
  ] 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号