使用哈希或 case 语句 [Ruby]

发布于 2024-08-31 21:02:30 字数 346 浏览 4 评论 0原文

一般来说,哪个更好用?:

case n
when 'foo'
 result = 'bar'
when 'peanut butter'
 result = 'jelly'
when 'stack'
 result = 'overflow'
return result

或者

map = {'foo' => 'bar', 'peanut butter' => 'jelly', 'stack' => 'overflow'}
return map[n]

更具体地说,什么时候应该使用 case 语句,什么时候应该简单地使用哈希?

Generally which is better to use?:

case n
when 'foo'
 result = 'bar'
when 'peanut butter'
 result = 'jelly'
when 'stack'
 result = 'overflow'
return result

or

map = {'foo' => 'bar', 'peanut butter' => 'jelly', 'stack' => 'overflow'}
return map[n]

More specifically, when should I use case-statements and when should I simply use a hash?

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

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

发布评论

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

评论(3

和我恋爱吧 2024-09-07 21:02:30

哈希是一种数据结构,而 case 语句是一种控制结构。

当您只是检索一些数据时(例如您提供的示例),您应该使用哈希。如果需要执行额外的逻辑,则应该编写 case 语句。

另外,如果您需要执行一些模式匹配,那么使用 case 语句是有意义的:


#pattern matching using ranges
letterGrade = case score
   when 0..64 then "F"
   when 65..69 then "D"
   when 70..79 then "C"
   when 80..89 then "B"
   when 90..100 then "A"
   else "Invalid Score"
end

#pattern matching using regular expressions
case songData
  when /title=(.*)/
    puts "Song title: #$1"
  when /track=(.*)/
    puts "Track number: #$1"
  when /artist=(.*)/
    puts "Artist name: #$1"
end


A hash is a data structure, and a case statement is a control structure.

You should use a hash when you are just retrieving some data (like in the example you provided). If there is additional logic that needs to be performed, you should write a case statement.

Also, if you need to perform some pattern matching, it makes sense to use a case statement:


#pattern matching using ranges
letterGrade = case score
   when 0..64 then "F"
   when 65..69 then "D"
   when 70..79 then "C"
   when 80..89 then "B"
   when 90..100 then "A"
   else "Invalid Score"
end

#pattern matching using regular expressions
case songData
  when /title=(.*)/
    puts "Song title: #$1"
  when /track=(.*)/
    puts "Track number: #$1"
  when /artist=(.*)/
    puts "Artist name: #$1"
end


‖放下 2024-09-07 21:02:30

一般来说,编程中的“更好”意味着不同的事情。例如,更好的程序

  1. 更容易理解,即
    更好地表达意图
  2. 更容易维护。例如,
    更少的代码行,更少
    容易出错等方面
  3. 具有更好的性能
    执行时间
  4. 具有更好的性能,就
    内存使用

等。

由于我们谈论的是 Ruby,因此性能通常不太受关注。如果您确实需要性能,您可以考虑另一种编程语言。因此,我首先考虑标准(1)和(2)。更好看的 Ruby 代码通常代表“更好”的程序。哪个代码看起来更好?哪个更能表达意图?如果添加/删除逻辑,哪一个更容易修改?这取决于您的问题,并且在某种程度上这是一个品味问题。

对我来说,在你的简短示例中,哈希解决方案更好。案例解决方案提供了更大的灵活性,在本例中您不需要(但在其他情况下可能需要)。

In general, "better" in programming means different things. For example, better program

  1. is easier to understand, i.e.
    expresses the intent better
  2. is easier to maintain. For example,
    less lines of code, less
    error-prone, etc.
  3. has better performance, in terms of
    execution time
  4. has better performance, in terms of
    memory usage

etc.

Since we are talking about Ruby, the performance is typically of a lesser concern. If you really need performance, you might consider another programming language. So, I would look at criteria (1) and (2) first. The better looking Ruby code usually represents a "better" program. Which code looks better? Which expresses the intent better? Which would be easier to modify if you add/remove logic? It depends on your problem, and it's a matter of taste, to certain degree.

To me, in your short example, the hash solution is better. The case solution provides more flexibility, which you don't need in this case (but might need in other cases).

烂人 2024-09-07 21:02:30

哈希表和 case 语句之间有两个主要区别。

  1. 哈希表可以在词法环境中存储、修改和重用,而 case 语句则不能。它产生一个结果,然后消失。但请注意,如果 case 语句位于块/方法内,则当然可以通过多次调用块/方法来多次使用它(但不容易修改)。
  2. 哈希表文字表达式始终计算其所有键和值,而 case 语句仅计算它需要的“键”和它将返回的“值”。这通常是更关键的区别,因为正如 Derek B. 指出的那样,这些值可能涉及额外的逻辑,如果只需要其中一个值,则不必要地执行这些逻辑的成本可能会非常高。

case n
when 'foo'
 'bar'
when 'peanut butter'
 'jelly'
when 'stack'
 'overflow'
end

这相当于你的代码,在使用 Ruby 或函数式编程语言一段时间后,它对你来说会显得更加自然。它也短得多。

There are two main differences between hash tables and case statements.

  1. A hash table can be stored, modified, and reused within the lexical environment, whereas a case statement cannot. It produces a result and then disappears. Note however that if the case statement is within a block/method, it can of course be used multiple times by calling the block/method multiple times (but not easily modified).
  2. A hash table literal expression always evaluates all of its keys and values, whereas a case statement only evaluates the "keys" it needs to and the "value" that it's going to return. This is often the more crucial difference, since the values could, as Derek B. points out, involve additional logic, which could be very costly to perform unnecessarily if only one of the values is needed.

.

case n
when 'foo'
 'bar'
when 'peanut butter'
 'jelly'
when 'stack'
 'overflow'
end

That's equivalent to your code, and after using Ruby or functional programming languages for a while, it will appear much more natural to you. It's also way shorter.

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