xx = if string.include?("1")
1
elsif host.include?("a")
"fd"
elsif host.include?("fd")
"wq"
elsif host.include?('na')
"sa"
elsif host.include?('op')
"l"
end
Refactorings
No refactoring yet !
Aaron Scruggs
August 20, 2009, August 20, 2009 05:40, permalink
terser, cleaner and expressiver =)
xx = case host
when /1/
1
when /a/
"fd"
when /fd/
"wq"
when /na/
"sa"
when /ap/
"l"
end
Mort
August 20, 2009, August 20, 2009 08:40, permalink
Doesn't cut down on length, but makes adding another match condition less ugly.
transforms = {
'1' => 1,
'a' => 'fd',
'fd' => 'wq',
'na' => 'sa',
'op' => '1'
}
xx = ""
transforms.each_pair do |k, v|
if string.include?(k)
xx = v
break
end
end
Mort
August 20, 2009, August 20, 2009 08:42, permalink
Oops, misread the original.
transforms = {
'a' => 'fd',
'fd' => 'wq',
'na' => 'sa',
'op' => '1'
}
xx = ""
if string.include?('1')
xx = "1"
else
transforms.each_pair do |k, v|
if host.include?(k)
xx = v
break
end
end
end
Martin Plöger
August 20, 2009, August 20, 2009 12:33, permalink
If the given example has a typing error with 'string' and 'host', so everything is 'host', I would prefer Aaron Scruggs solution. Otherwise I have 3 other variants.
if string.include? '1'
1
else
{ 'a' => 'fd',
'fd' => 'wq',
'na' => 'sa',
'op' => '1'
}.map { |k, v| v if host.include?(k) }.compact.first
#.find { |k, v| host.include? k }.last also works!
end
#Ruby1.9
if string.include? '1'
1
else
{a: 'fd', fd: 'wq', na: 'sa', op: '1'}.map { |k, v| v if host.include?(k) }.compact.first
end
#Like Aaron's solution (but with a different variable 'string' for the 1st line:
xx = case true
when string =~ /1/ then 1
when host =~ /a/ then 'fd'
when host =~ /fd/ then 'wq'
when host =~ /na/ then 'sa'
when host =~ /ap/ then 'l'
end
steved
August 20, 2009, August 20, 2009 16:55, permalink
def host_to_xx(host)
host_maps = {'a' => 'fd', 'fd' => 'wq', 'na' => 'sa', 'op' => 'l'}
host_maps.each do |key, value|
return value if host.include?(key)
end
end
puts host_to_xx('fd')
Muke Tever
August 21, 2009, August 21, 2009 00:37, permalink
Tried to make it look nicer, if nothing else. Some of the answers posted call regexps too often for my taste ;) You could use a hash, yeah, but you also asked for a case statement with a regular expression, so here you go.
[Edited because the regexp didn't need to be in its own variable really...]
xx = string.include?("1") ? 1 :
case host[/(a|fd|na|op)/]
when 'a' then "fd"
when 'fd' then "wq"
when 'na' then "sa"
when 'op' then "l"
end
Muke Tever
August 21, 2009, August 21, 2009 15:14, permalink
And here's the same thing without a separate statement to check 'string'.
xx = case string[/1/] || host[/(a|fd|na|op)/]
when '1' then 1
when 'a' then "fd"
when 'fd' then "wq"
when 'na' then "sa"
when 'op' then "l"
end
mmcis-investments.com
September 15, 2009, September 15, 2009 15:06, permalink
Want to have stable and high income, but cannot imagine where to invest your funds? We already have found a solution - MMCIS investments is the most profitable decision! Your money will be invested into innovative projects worldwide and you will receive monthly income by several times exceeding bank interest rate! We diversify our assets greatly, which allows us to minimize risks. We will make our Fund your reliable partner for years. Make a deposit now and appreciate all benefits of cooperating with our investment fund!
is any some short-hand for this, like using hash or others?