I'm trying to convert special symbols ({W} {B} {U} etc) to their respective color so I wrote a CASE but I'm thinking a case isn't what I need since it ends as soon as it finds a match.
print 'Test a Color'
color = gets.chomp
case color
when '{W}'
puts 'White'
when '{R}'
puts 'Red'
when '{G}'
puts 'Green'
when '{U}'
puts 'Blue'
when '{B}'
puts 'Black'
end
{B} gets me Black, {U} gets my Blue. {U}{B} crashes it/returns nothing. How would I go about letting it continue down the list?
Check below.
print "Test a Color"
color = gets.chomp
hash = {
'{W}' => 'White',
'{R}' => 'Red'
}
# finding by regex and replace with what you want.
puts color.gsub(/\{.+?\}/){|k| hash[k] || k }
colors = {
'W' => 'White',
'R' => 'Red',
'G' => 'Green',
'U' => 'Blue',
'B' => 'Black',
}
input.scan(/{(\w)}/).each { |abbreviation| puts colors[*abbreviation] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With