ruby - Is it possible to conditionally add item to OpenStruct? -
i want add string openstruct this:
link = link.split(",") openstruct.new(title: link[0].strip, url: link[1].strip)
sometimes, contains third variable, want add well:
openstruct.new(title: link[0].strip, url: link[1].strip, id: link[2].strip)
i check link[2]
, create 2 openstruct.new
s lines, there way add id afterwards?
prepare hash , modify that conditionally. unconditionally pass openstruct.
link = link.split(',') os_args = { title: link[0].strip, url: link[1].strip } os_args[:id] = link[2].strip if link[2] openstruct.new(os_args)
Comments
Post a Comment