3

We can easily convert a keyword into a string:

true.to_s
=> "true"

But how to convert a string into a keyword?

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • I'm using Cucumber and I'm passing "true" as the block parameter. Would be nice to be able to get the true keyword. – sivabudh Oct 06 '11 at 13:37
  • Thanks Bohdan. I just thought it would be nice to be able to write something like: Fabricate :my_model, :correct => true_string.to_keyword ...where :correct attribute is a boolean type. – sivabudh Oct 06 '11 at 13:44

5 Answers5

6

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
knut
  • 27,320
  • 6
  • 84
  • 112
3

try this:

ruby-1.9.2-p136 :001 > true
 => true 
ruby-1.9.2-p136 :002 > eval("true")
 => true
ben
  • 1,819
  • 15
  • 25
  • 4
    But don't forget [evil is eval](http://stackoverflow.com/questions/637421/is-eval-supposed-to-be-nasty) – knut Oct 06 '11 at 16:25
  • 3
    Please DO NOT do this! It's very dangerous!!! Consider this. If this is in a model which its arguments are passed through the controller (which is almost always), a malicious attach could eval anything from what you think is only a checkbox. – Adit Saxena Jan 31 '14 at 11:40
2

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
knut
  • 27,320
  • 6
  • 84
  • 112
1

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")
earlonrails
  • 4,966
  • 3
  • 32
  • 47
-2

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
...
christianblais
  • 2,448
  • 17
  • 14