这是编写 Ruby 代码的最佳方式吗?

发布于 2024-10-29 06:20:40 字数 352 浏览 2 评论 0原文

有没有更好的方法来编写这个 Ruby 代码?请帮忙重构。

def get_code(val)
    case val
    when 0..20 then 'E2'
    when 20..32 then 'E1'
    when 33..40 then 'D'
    when 41..50 then 'C2'
    when 51..60 then 'C1'
    when 61..70 then 'B2'
    when 71..80 then 'B1'
    when 81..90 then 'A2'
    when 91..100 then 'A1'
    else 'A0'
    end
 end

Is there a better way to write this Ruby code? Please help to refactor.

def get_code(val)
    case val
    when 0..20 then 'E2'
    when 20..32 then 'E1'
    when 33..40 then 'D'
    when 41..50 then 'C2'
    when 51..60 then 'C1'
    when 61..70 then 'B2'
    when 71..80 then 'B1'
    when 81..90 then 'A2'
    when 91..100 then 'A1'
    else 'A0'
    end
 end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空名 2024-11-05 06:20:40

你的代码非常好。
为了好玩,您可以使用散列而不是 case

def get_code(val)
  my_hash = { 0..20 => 'E2',
    20..32 => 'E1',
    33..40 => 'D',
    41..50 => 'C2',
    51..60 => 'C1',
    61..70 => 'B2',
    71..80 => 'B1',
    81..90 => 'A2',
    91..100 => 'A1' }
  my_hash.select{ |k, v| k === val }.first[1] rescue 'A0'
end

或者使其更加内联:)

def get_code(val)
  { 0..20 => 'E2', 20..32 => 'E1', 33..40 => 'D', 41..50 => 'C2', 51..60 => 'C1', 61..70 => 'B2', 71..80 => 'B1', 81..90 => 'A2', 91..100 => 'A1' }.select{ |k, v| k === val }.first[1] rescue 'A0'
end

,并且正如@Geo 注意到的那样,您应该从方法中提取哈希值。

Your code is quite good.
For fun you can use hash instead of case.

def get_code(val)
  my_hash = { 0..20 => 'E2',
    20..32 => 'E1',
    33..40 => 'D',
    41..50 => 'C2',
    51..60 => 'C1',
    61..70 => 'B2',
    71..80 => 'B1',
    81..90 => 'A2',
    91..100 => 'A1' }
  my_hash.select{ |k, v| k === val }.first[1] rescue 'A0'
end

Or to make it more inline :)

def get_code(val)
  { 0..20 => 'E2', 20..32 => 'E1', 33..40 => 'D', 41..50 => 'C2', 51..60 => 'C1', 61..70 => 'B2', 71..80 => 'B1', 81..90 => 'A2', 91..100 => 'A1' }.select{ |k, v| k === val }.first[1] rescue 'A0'
end

and, as @Geo noticed, you should extract your hash out of method.

心欲静而疯不止 2024-11-05 06:20:40
def get_code(val)
    if [31, 32].include?(val) then 'E1'
    else ['E2', 'E2', 'E1', 'D', 'C2', 'C1', 'B2', 'B1', 'A2', 'A1'][(val-1)/10] || 'A0'
    end
end

我不确定你是如何处理 0 负数的。它看起来像一个分级系统。如果我的代码有错误,我想你能够修复它。

更新
另一个版本:

def get_code(val)
    val += 1 if 0 == val
    val -= 2 if [31, 32].include?(val)
    ['E2', 'E2', 'E1', 'D', 'C2', 'C1', 'B2', 'B1', 'A2', 'A1'][(val-1)/10] || 'A0'
end
def get_code(val)
    if [31, 32].include?(val) then 'E1'
    else ['E2', 'E2', 'E1', 'D', 'C2', 'C1', 'B2', 'B1', 'A2', 'A1'][(val-1)/10] || 'A0'
    end
end

I am not sure about how you are dealing with 0, negatives. It looks like a grading system. If there is a mistake to my code, I think you will be able to fix it.

Update
Another version:

def get_code(val)
    val += 1 if 0 == val
    val -= 2 if [31, 32].include?(val)
    ['E2', 'E2', 'E1', 'D', 'C2', 'C1', 'B2', 'B1', 'A2', 'A1'][(val-1)/10] || 'A0'
end
夏见 2024-11-05 06:20:40

不是重构,只是一种更短的编写方式:

def get_code(val)
    case val
    when 0..20
      'E2'
    when 20..32
      'E1'
    when 33..40
      'D'
    when 41..50
      'C2'
    # etc.
    else
      'A0'
    end
 end

或者您可以用分号替换 then

case val
  when  0..20; 'E2'
  when 20..32; 'E1'
  #etc
  else 'A0'
end

Not a refactoring, just a shorter way to write it:

def get_code(val)
    case val
    when 0..20
      'E2'
    when 20..32
      'E1'
    when 33..40
      'D'
    when 41..50
      'C2'
    # etc.
    else
      'A0'
    end
 end

Or you could substitute the then's with semi-colons:

case val
  when  0..20; 'E2'
  when 20..32; 'E1'
  #etc
  else 'A0'
end
爱给你人给你 2024-11-05 06:20:40

如果这是该模式的唯一实例,则不值得缩短;它非常清晰,而且不是很吵。

If this is the only instance of this pattern, then it's not worth shortening; it's perfectly clear as it is, and not very noisy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文