Ruby 匹配子集并显示集合(变量)

发布于 2024-12-29 19:02:29 字数 447 浏览 0 评论 0原文

feelings = Set["happy", "sad", "angry", "high", "low"]
euphoria = Set["happy", "high"]
dysphoria = Set["sad", "low"]
miserable = Set["sad", "angry"]

puts "How do you feel?"
str = gets.chomp
p terms = str.split(',')

if euphoria.proper_subset? feelings
  puts "You experiencing a state of euphoria."
else
  puts "Your experience is undocumented."
end

gets

如何使 euphoria 成为一个变量,这样如果悲惨或烦躁的相应字符串匹配 &显示设定名称。就像#{设置}

feelings = Set["happy", "sad", "angry", "high", "low"]
euphoria = Set["happy", "high"]
dysphoria = Set["sad", "low"]
miserable = Set["sad", "angry"]

puts "How do you feel?"
str = gets.chomp
p terms = str.split(',')

if euphoria.proper_subset? feelings
  puts "You experiencing a state of euphoria."
else
  puts "Your experience is undocumented."
end

gets

How do I make euphoria a variable, such that if the corresponding string for miserable or dysphoria match & display the set name. Like #{Set}

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

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

发布评论

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

评论(2

记忆里有你的影子 2025-01-05 19:02:29

回顾一下你所拥有的,我认为这更像是你真正想要的:

require 'set'

feelings = {
  euphoria: Set.new(%w[happy high]),
 dysphoria: Set.new(%w[sad low]),
 miserable: Set.new(%w[sad angry])
}

puts "What are you feeling right now?"
mood = Set.new gets.scan(/\w+/)
name, _ = feelings.find{ |_,matches| matches.subset?( mood ) }
if name
  puts "You are experiencing a state of #{name}"
else
  puts "Your experience is undocumented."      
end

调用 gets.scan(/\w+/) 返回一个字符串数组。它比 .split(',') 更好,因为它允许用户在逗号后面添加空格(例如“sad, happy”)或仅使用空格(例如“sad happy”)。

正如您所知,Set[] 需要多个参数。相反,我们使用 Set.new 它接受一个值数组。或者,您可以使用 mood = Set[*gets.scan(/\w+/)],其中 * 获取值数组并将它们作为显式参数传递。

另外,我从 proper_subset? 更改为 subset?,因为“happy,high”不是“happy,high”的正确子集,但它是一个子集。

Reviewing what you have, I think this is more like what you really want:

require 'set'

feelings = {
  euphoria: Set.new(%w[happy high]),
 dysphoria: Set.new(%w[sad low]),
 miserable: Set.new(%w[sad angry])
}

puts "What are you feeling right now?"
mood = Set.new gets.scan(/\w+/)
name, _ = feelings.find{ |_,matches| matches.subset?( mood ) }
if name
  puts "You are experiencing a state of #{name}"
else
  puts "Your experience is undocumented."      
end

Calling gets.scan(/\w+/) returns an array of strings. It's better than just .split(',') because it allows the user to put a space after commas (e.g. "sad, happy") or just use spaces (e.g. "sad happy").

As you already know, Set[] requires multiple arguments for it. Instead, we use Set.new which takes an array of values. Alternatively, you could have used mood = Set[*gets.scan(/\w+/)], where the * takes the array of values and passes them as explicit parameters.

Also, I changed from proper_subset? to just subset?, because "happy,high" is not a proper subset of "happy,high", but it is a subset.

拥有 2025-01-05 19:02:29

每当您认为要将一个变量的名称放入另一个变量时,您可能需要一个哈希值:

states = {
    'euphoria'  => Set["happy", "high"],
    'dysphoria' => Set["sad",   "low"],
    'miserable' => Set["sad",   "angry"]
}

然后您可以这样说:

which = 'euphoria' # Or where ever this comes from...
if states[which].proper_subset? feelings
  puts "You experiencing a state of #{which}."
else
  puts "Your experience is undocumented."
end

Whenever you think you want to put the name of a variable into another variable, you probably want a Hash instead:

states = {
    'euphoria'  => Set["happy", "high"],
    'dysphoria' => Set["sad",   "low"],
    'miserable' => Set["sad",   "angry"]
}

Then you can say things like:

which = 'euphoria' # Or where ever this comes from...
if states[which].proper_subset? feelings
  puts "You experiencing a state of #{which}."
else
  puts "Your experience is undocumented."
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文