[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: symbol to true object

Brian Candler

2/26/2007 9:39:00 AM

On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir Khan 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]

4 Answers

Hans Sjunnesson

2/26/2007 10:02:00 AM

0

On Feb 26, 10:39 am, Brian Candler <B.Cand...@pobox.com> wrote:
> On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir Khan 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

--
Hans

Logan Capaldo

2/26/2007 2:09:00 PM

0

On Mon, Feb 26, 2007 at 07:05:05PM +0900, Hans Sjunnesson wrote:
> On Feb 26, 10:39 am, Brian Candler <B.Cand...@pobox.com> wrote:
> > On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir Khan 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")
if eql?(:true)
> true
> elsif to_s.eql?("false")
elsif eql?(:false)
> false
> else
> raise ArgumentError
> end
> end
> end
>
> :true.to_bool => true
> :false.to_bool => false
> :nothing.to_bool => ArgumentError
>
There, fixed that for you :)
> --
> Hans
>

Hans Sjunnesson

2/26/2007 2:19:00 PM

0

On Feb 26, 3:08 pm, Logan Capaldo <logancapa...@gmail.com> wrote:
> On Mon, Feb 26, 2007 at 07:05:05PM +0900, Hans Sjunnesson wrote:
> > On Feb 26, 10:39 am, Brian Candler <B.Cand...@pobox.com> wrote:
> > > On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir Khan 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")
>
> if eql?(:true)> true
> > elsif to_s.eql?("false")
>
> elsif eql?(:false)> false
> > else
> > raise ArgumentError
> > end
> > 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 :-)

--
Hans

Brian Candler

2/26/2007 6:25:00 PM

0

On Tue, Feb 27, 2007 at 12:57:30AM +0900, Gareth Adams 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