Forum: Ruby symbol to true object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nasir K. (Guest)
on 2007-02-26 09:19
(Received via mailing list)
Hi,
Is there an idiom to convert the symbol :true to object true (of
TrueClass)?


Basically how can I better

  def  validate_the_message (sym_arg)
     @vtm =
     case sym_arg
     when  :true
        true
      when :false
        false
      else
         # do something else
      end
  end

(assume @vtm an instance variable holding the choice.)

TIA
Brian C. (Guest)
on 2007-02-26 11:40
(Received via mailing list)
On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir K. wrote:
> Is there an idiom to convert the symbol :true to object true (of TrueClass)?

BOOL_MAP = { :true=>true, :false=>false }
BOOL_MAP[sym_arg]
Hans S. (Guest)
on 2007-02-26 12:05
(Received via mailing list)
On Feb 26, 10:39 am, Brian C. <removed_email_address@domain.invalid> wrote:
> On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir K. wrote:
> > Is there an idiom to convert the symbol :true to object true (of TrueClass)?
>
> BOOL_MAP = { :true=>true, :false=>false }
> BOOL_MAP[sym_arg]

Or

class Symbol
  def to_bool
    if to_s.eql?("true")
      true
    elsif to_s.eql?("false")
      false
    else
      raise ArgumentError
    end
  end
end

:true.to_bool => true
:false.to_bool => false
:nothing.to_bool => ArgumentError
Logan C. (Guest)
on 2007-02-26 16:09
(Received via mailing list)
On Mon, Feb 26, 2007 at 07:05:05PM +0900, Hans S. wrote:
>   def to_bool
>     if to_s.eql?("true")
      if eql?(:true)
>       true
>     elsif to_s.eql?("false")
      elsif eql?(:false)
>
There, fixed that for you :)
Hans S. (Guest)
on 2007-02-26 16:21
(Received via mailing list)
On Feb 26, 3:08 pm, Logan C. <removed_email_address@domain.invalid> wrote:
> > class Symbol
> >   end
> > end
>
> > :true.to_bool => true
> > :false.to_bool => false
> > :nothing.to_bool => ArgumentError
>
> There, fixed that for you :)
>
> > --
> > Hans

Ah oops, thank your for that :-)
Gareth A. (Guest)
on 2007-02-26 17:58
(Received via mailing list)
> > >     else
> > >       raise ArgumentError
> > >     end
> > >   end
> > > end
> >
> > > :true.to_bool => true
> > > :false.to_bool => false
> > > :nothing.to_bool => ArgumentError

class Symbol
  def to_bool
    case to_s
      when "true" then true
      when "false" then false
      else raise ArgumentError
    end
  end
end
Brian C. (Guest)
on 2007-02-26 20:26
(Received via mailing list)
On Tue, Feb 27, 2007 at 12:57:30AM +0900, Gareth A. wrote:
> class Symbol
>   def to_bool
>     case to_s
>       when "true" then true
>       when "false" then false
>       else raise ArgumentError
>     end
>   end
> end

Why to_s?

class Symbol
  def to_bool
    case self
      when :true then true
      when :false then false
      else raise ArgumentError
    end
  end
end
This topic is locked and can not be replied to.