Reputation: 1
case look
when 'side'
hash1 = {
'num' => 5,
'style' => 'sideStyle',
}
when 'front'
hash1 = {
'num' => 8,
'style' => 'frontStyle',
}
when 'back'
hash1 = {
'num' => 4,
'style' => 'backStyle',
'special' => 'yes',
}
else
hash1 = {
'num' => 2,
'style' => 'noStyle',
}
end
myStyle = Hash[hash1]
My piece of code looks like this.
when I run this code I get "odd number of arguments for Hash".
is this the correct way to from an hash? could someone please help me how to get it resolved.
Upvotes: 0
Views: 2413
Reputation: 121000
Too much of a hash initializations in a row. Just assign the result of case
to the value:
my_style =
case look
when 'side'
{
'num' => 5,
'style' => 'sideStyle',
}
when 'front'
{
'num' => 8,
'style' => 'frontStyle',
}
when 'back'
{
'num' => 4,
'style' => 'backStyle',
'special' => 'yes',
}
else
{
'num' => 2,
'style' => 'noStyle',
}
end
To DRY, I personally would better do:
result =
case look
when 'side' then [5, "sideStyle"]
when 'front' then [8, "frontStyle"]
when 'back' then [4, "backStyle", "yes"]
else [2, "noStyle"]
end
result.zip(%w|num style special|).map(&:rotate).to_h
Upvotes: 1