DEV Community

Yee
Yee

Posted on

Conditional building of Ruby Array and Hash

[].tap do |a|
a << :first if true
end

{}.tap do |h|
h[:first] = 1 if true
end

{
a: 'a',
b: ('b' if true)
}.compact

[1, 2, *(3 if true)]

Top comments (0)