Created
August 27, 2025 19:38
-
-
Save jameskerr/292ded38fdbb774e159279bd8e9523cc to your computer and use it in GitHub Desktop.
Compose Attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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