将字符串转换为关键字

发布于 2024-12-08 15:12:40 字数 99 浏览 0 评论 0原文

我们可以轻松地将关键字转换为字符串:

true.to_s
=> "true"

但是如何将字符串转换为关键字呢?

We can easily convert a keyword into a string:

true.to_s
=> "true"

But how to convert a string into a keyword?

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

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

发布评论

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

评论(5

过气美图社 2024-12-15 15:12:40

你有多少个关键词?您对“关键字”的定义是什么?

我会用 case-命令来实现。您可以为字符串定义一个 to_keyword 方法。我的实现检测 true、false、nil(或 NULL)。检测到字符串,忽略大写字母(TRUE 也将是 true)。其他字符串将返回一个符号(字符串本身将是另一个合理的结果)。

该示例可以适用于更多“关键字”或其他结果。

class String
  #Return 'keyword'
  #Detects:
  #- true (independend of lower letters/capitals)
  #- false (independend of lower letters/capitals)
  #- nil/NULL (independend of lower letters/capitals)
  def to_keyword
    case self
      when /\Atrue\Z/i; true
      when /\Afalse\Z/i; false
      when /\Anil\Z/i, /\ANULL\Z/; nil
      else; self.to_sym #return symbol. Other posibility: self.
    end
  end
end


p 'true'.to_keyword #true
p 'TRUE'.to_keyword #true
p 'false'.to_keyword #false
p 'NULL'.to_keyword #nil  (NULL is used in DB like nil)
p 'NULLc'.to_keyword #:NULLc  not detected -> symbol

How many keywords do you have? What's your definition of a 'keyword'?

I would implement with a case-command. You may define a to_keyword method for String. My implementation detects true, false, nil (or NULL). The strings are detected, ignoring capitals (TRUE will also be true) Other strings will return a symbol (The string itself would be another reasonable result).

The example can be adapted for further 'keywords' or other results.

class String
  #Return 'keyword'
  #Detects:
  #- true (independend of lower letters/capitals)
  #- false (independend of lower letters/capitals)
  #- nil/NULL (independend of lower letters/capitals)
  def to_keyword
    case self
      when /\Atrue\Z/i; true
      when /\Afalse\Z/i; false
      when /\Anil\Z/i, /\ANULL\Z/; nil
      else; self.to_sym #return symbol. Other posibility: self.
    end
  end
end


p 'true'.to_keyword #true
p 'TRUE'.to_keyword #true
p 'false'.to_keyword #false
p 'NULL'.to_keyword #nil  (NULL is used in DB like nil)
p 'NULLc'.to_keyword #:NULLc  not detected -> symbol
如歌彻婉言 2024-12-15 15:12:40

试试这个:

ruby-1.9.2-p136 :001 > true
 => true 
ruby-1.9.2-p136 :002 > eval("true")
 => true

try this:

ruby-1.9.2-p136 :001 > true
 => true 
ruby-1.9.2-p136 :002 > eval("true")
 => true
不及他 2024-12-15 15:12:40

你可以尝试yaml:

require "yaml"
p YAML.load('true')
p YAML.load('TRUE')
p YAML.load('false')
p YAML.load('nil')
p YAML.load('NULL') #nil

You could try yaml:

require "yaml"
p YAML.load('true')
p YAML.load('TRUE')
p YAML.load('false')
p YAML.load('nil')
p YAML.load('NULL') #nil
橪书 2024-12-15 15:12:40

我最喜欢克努特的答案。但我认为我不会支持“Null”和其他内容。
这是这个版本,稍微简单一些。

class String
  def to_keyword
    self == "true"
  end
end

>> "true".to_keyword
=> true
>> "false".to_keyword
=> false

不过这个问题非常简单。在你的测试中你可以简单地

:correct => (true_string == "true")

I like Knut's answers mostly. I don't think I would support "Null" and others though.
Here is this version which is a little more simple.

class String
  def to_keyword
    self == "true"
  end
end

>> "true".to_keyword
=> true
>> "false".to_keyword
=> false

This problem is pretty straight forward though. In your tests you could simply

:correct => (true_string == "true")
暖阳 2024-12-15 15:12:40

根据您的评论,您可以执行类似的操作:

true_string = "true"

:correct => !!true_string

# examples
!true_string #=> false
!!true_string #=> true
!!!true_string #=> false
...

Following your comment, you could do something like that :

true_string = "true"

:correct => !!true_string

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