Professional Web Applications Themes

boolean to int conversion - Ruby

I'm a bit of a Ruby newbie so be kind pls. Could someone tell me if there's an easy way to make a value of True go to 1 and False goto 0? a sort of to_i method? Thanks, Cere...

  1. #1

    Default boolean to int conversion


    I'm a bit of a Ruby newbie so be kind pls.

    Could someone tell me if there's an easy way to make a value of True go to 1 and False goto 0?

    a sort of to_i method?

    Thanks,
    Cere


    Cere Guest

  2. #2

    Default Re: boolean to int conversion

    A few different ways:

    1)
    v = value ? 1 : 0

    where value is either true or false
    2)

    class TrueClass; def to_i; 1; end; end
    class FalseClass; def to_i; 0; end; end



    Evan Webb // net

    On Tue, 2004-02-17 at 19:09, Cere Davis wrote: 



    Evan Guest

  3. #3

    Default Re: boolean to int conversion

    Cere Davis wrote:
     

    Besides adding them yourself, I don't think so. However, you can use
    the ternary operator to good effect here:

    a = true
    puts( a ? 1 : 0 )
    a = false
    puts( a ? 1 : 0 )

    --
    Jamis Buck
    byu.edu
    http://www.jamisbuck.org/blog/jamis.cgi

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  4. #4

    Default Re: boolean to int conversion

    On Tue, 17 Feb 2004, Cere Davis wrote:
     

    [true, false].each |bool| do
    int = (bool ? 1 : 0)
    p int
    end

    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================


    Ara.T.Howard Guest

  5. #5

    Default Re: boolean to int conversion


    "Evan Webb" <net> schrieb im Newsbeitrag
    news:.. 

    In fact "value" might be anything with "false" and "nil" meaning "false"
    (and evaluating to "0" in this case) and everything else meaning "true"
    (yields "1").

    Regards

    robert

     [/ref]
    go to 1 and False goto 0? 
    >
    >
    >[/ref]

    Robert Guest

  6. #6

    Default Re: boolean to int conversion

    Thanks everyone for help on this...

    Ara.T.Howard wrote: 
    >
    >
    > [true, false].each |bool| do
    > int = (bool ? 1 : 0)
    > p int
    > end
    >
    > -a[/ref]
    Cere Guest

Similar Threads

  1. Flex CFC Boolean
    By pope on acid in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 8th, 01:11 PM
  2. #39904 [NEW]: string -> boolean conversion of "\0" could give FALSE
    By zizka at seznam dot cz in forum PHP Bugs
    Replies: 0
    Last Post: December 20th, 02:46 PM
  3. boolean cfsqltype
    By doramon in forum Coldfusion Database Access
    Replies: 3
    Last Post: April 8th, 01:49 AM
  4. Boolean constructor...
    By jmoliere in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 6th, 03:09 PM
  5. boolean operation
    By Stijn Goris in forum PHP Development
    Replies: 4
    Last Post: October 22nd, 02:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139