'true' asBoolean.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

'true' asBoolean.

Sean Malloy-2
Is there such any way to take a string representation of a Boolean, and turn
it into a boolean?.. There is no asBoolean method on String, Boolean
contains no class methods for converting a string to a boolean... I know I
could go and define my own method (yay smalltalk)

Is it just easier to go

| option |
option := (dictionary at: 'compress') = 'true'.

rather than something like

| option |
option := (dictionary at: 'compress') asBoolean.

Its coming back to parsing a connection string. Once the string is parsed,
and each component is broken up into Associations in a dictionary, each key
/ value are strings. But in some instances I want to treat a value as a
Number (asNumber on string), but some as boolean options.

I guess I could change an option from 'true' to '1', and then do '1'
asNumber asBoolean, but tyat just seems plain wrong.....

I keep wanting to write a class similar to the C# Convert class..

option := Convert toBoolean: (dictionary at: 'compress').

or something. What does everyone else do?


Reply | Threaded
Open this post in threaded view
|

Re: 'true' asBoolean.

Blair McGlashan
"Sean Malloy" <[hidden email]> wrote in message
news:[hidden email]...
> Is there such any way to take a string representation of a Boolean, and
turn
> it into a boolean?.. There is no asBoolean method on String, Boolean
> contains no class methods for converting a string to a boolean... I know I
> could go and define my own method (yay smalltalk)
>...
> I keep wanting to write a class similar to the C# Convert class..
>
> option := Convert toBoolean: (dictionary at: 'compress').
>
> or something. What does everyone else do?

You can use the BooleanToText converter, e.g.

    BooleanToText new rightToLeft: 'true'

#leftToRight: does the opposite conversion.

Regards

Blair


jas
Reply | Threaded
Open this post in threaded view
|

Re: 'true' asBoolean.

jas
In reply to this post by Sean Malloy-2
Sean Malloy wrote:

> Is there such any way to take a string representation of a Boolean, and turn
> it into a boolean?.. There is no asBoolean method on String, Boolean
> contains no class methods for converting a string to a boolean... I know I
> could go and define my own method (yay smalltalk)
>
> Is it just easier to go
>
> | option |
> option := (dictionary at: 'compress') = 'true'.
>
> rather than something like
>
> | option |
> option := (dictionary at: 'compress') asBoolean.
>
> Its coming back to parsing a connection string. Once the string is parsed,
> and each component is broken up into Associations in a dictionary, each key
> / value are strings. But in some instances I want to treat a value as a
> Number (asNumber on string), but some as boolean options.
>
> I guess I could change an option from 'true' to '1', and then do '1'
> asNumber asBoolean, but tyat just seems plain wrong.....
>
> I keep wanting to write a class similar to the C# Convert class..
>
> option := Convert toBoolean: (dictionary at: 'compress').
>
> or something. What does everyone else do?

====
>>slurp: connectionString
    " Answer dictionary of {keys -> boolean, number, or string} "
    (bools := Dictionary new) at: 'true' put: true at: 'false' put: false.
    resolved := [:boolean|
                              [:value|
                                boolean
                                    at: value asLowercase
                                    ifAbsent: [value isNumeric
                                                       ifTrue:  [value asNumber]

                                                       ifFalse: [value]
                      ]       ]                   ] value: bools.

    ^(self parseIntoAssociations: connectionString)
           inject: Dictionary new
           into: [:dictionary :component|
                    dictionary
                        at: component key
                        put: (resolved value: component value)
                        ; yourself
                  ]

====
-cstb


Reply | Threaded
Open this post in threaded view
|

Re: 'true' asBoolean.

Yar Hwee Boon-3
In reply to this post by Sean Malloy-2
"Sean Malloy" <[hidden email]> wrote in message news:<[hidden email]>...
> Is there such any way to take a string representation of a Boolean, and turn
> it into a boolean?.. There is no asBoolean method on String, Boolean

BooleanToText perhaps?

Hwee Boon


jas
Reply | Threaded
Open this post in threaded view
|

Re: 'true' asBoolean - OAOO.

jas
In reply to this post by Sean Malloy-2
Sean Malloy wrote:

> Is there such any way to take a string representation of a Boolean, and turn
> it into a boolean?.. There is no asBoolean method on String, Boolean
> contains no class methods for converting a string to a boolean... I know I
> could go and define my own method (yay smalltalk)
>
> Is it just easier to go
>
> | option |
> option := (dictionary at: 'compress') = 'true'.
>
> rather than something like
>
> | option |
> option := (dictionary at: 'compress') asBoolean.
>
> Its coming back to parsing a connection string. Once the string is parsed,
> and each component is broken up into Associations in a dictionary, each key
> / value are strings. But in some instances I want to treat a value as a
> Number (asNumber on string), but some as boolean options.

Do the conversions immediately upon parsing
the input, do not store encoded strings.

You can always get the string rep again if needed.

-cstb