Ab36cdd7a51942c562ae942a0dd07b45

is any some short-hand for this, like using hash or others?

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 !

4652e329c029288c75003c3e544be438

Aaron Scruggs

August 20, 2009, August 20, 2009 05:40, permalink

1 rating. Login to rate!

terser, cleaner and expressiver =)

xx = case host
    when /1/
      1
    when /a/
      "fd"
    when /fd/
      "wq"
    when /na/
      "sa"
    when /ap/
      "l"
    end
C1f7bc8064161e7408ef62d97bb636ac

Mort

August 20, 2009, August 20, 2009 08:40, permalink

1 rating. Login to rate!

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
C1f7bc8064161e7408ef62d97bb636ac

Mort

August 20, 2009, August 20, 2009 08:42, permalink

No rating. Login to rate!

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
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

August 20, 2009, August 20, 2009 12:33, permalink

1 rating. Login to rate!

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
D41d8cd98f00b204e9800998ecf8427e

steved

August 20, 2009, August 20, 2009 16:55, permalink

1 rating. Login to rate!
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')
F4192eeb4b26e96deab8b5c68926105d

Muke Tever

August 21, 2009, August 21, 2009 00:37, permalink

No rating. Login to rate!

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
F4192eeb4b26e96deab8b5c68926105d

Muke Tever

August 21, 2009, August 21, 2009 15:14, permalink

1 rating. Login to rate!

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
8a23a68c44b1445e8de323c9349dc018

mmcis-investments.com

September 15, 2009, September 15, 2009 15:06, permalink

No rating. Login to rate!

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!

Your refactoring





Format Copy from initial code

or Cancel