Skip to content

Instantly share code, notes, and snippets.

@jameskerr
Created August 27, 2025 19:38
Show Gist options
  • Save jameskerr/292ded38fdbb774e159279bd8e9523cc to your computer and use it in GitHub Desktop.
Save jameskerr/292ded38fdbb774e159279bd8e9523cc to your computer and use it in GitHub Desktop.
Compose Attributes
def compose_attrs(a, b)
result = a.dup
b.each do |key, val|
x = result[key]
y = val
pair = [ x, y ]
if pair.any?(&:nil?)
result[key] = x || y
elsif pair.all? Hash
result[key] = compose_attrs(x, y)
elsif pair.all? String
result[key] = [ x, y ].join(" ")
else
result[key] = val
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment