Professional Web Applications Themes

Question on the flip/flop op - Ruby

With the following code where 'f' is an file handle: f.each_line{ |line| header = 1 .. $.=~$^ ? true : false body = $^ .. f.eof ? true : false } Say body becomes true after the first \n. What is stopping header from becoming true again on the next \n? Since it will be between 1 and the $^. Is the flip flop op somehow storing that $^ was already reached and not to test the condition again? Thanks, Zach --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.576 / Virus Database: 365 ...

  1. #1

    Default Question on the flip/flop op

    With the following code where 'f' is an file handle:

    f.each_line{ |line|
    header = 1 .. $.=~$^ ? true : false
    body = $^ .. f.eof ? true : false }

    Say body becomes true after the first \n. What is stopping header from
    becoming true again on the next \n? Since it will be between 1 and the $^.
    Is the flip flop op somehow storing that $^ was already reached and not to
    test the condition again?

    Thanks,

    Zach




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.576 / Virus Database: 365 - Release Date: 1/30/2004



    Zach Guest

  2. #2

    Default Re: Question on the flip/flop op


    "Zach Dennis" <com> schrieb im Newsbeitrag
    news:com... 

    First of all, you can use FF only in if statements. Then, using "?:" to
    convert boolean values to boolean values is superfluous. And btw your code
    doesn't compile for me.
     

    The behavior of the FF is like this: it's false until the first condition
    matches. If that's the case it becomes true and stays true as long as the
    second doesn't match. After that it's false until the first condition
    matches again.

    The easiest way to separate header and body of a file in mbox format is
    possibly this:

    io.each_line do |line|
    line.chomp!

    if /^From / =~ line .. /^$/ =~ line
    puts "Header: " + line unless line.length == 0
    else
    puts "Body: " + line
    end
    end

    Of course you will have to do more processing to treat individual header
    lines and deal with continued header lines. You can look into gurgitate
    mail to see how it does it.

    http://rubyforge.org/projects/gurgitate-mail/

    Regards

    robert

    Robert Guest

  3. #3

    Default Re: Question on the flip/flop op

    Robert Klemme wrote:
     

    Do you mean "only as a scalar"? FF's work just fine in other statements; "while" comes to mind.




    Michael Guest

  4. #4

    Default Re: Question on the flip/flop op


    "Michael campbell" <com> schrieb im Newsbeitrag
    news:com... 
    >
    > Do you mean "only as a scalar"? FF's work just fine in other statements;[/ref]
    "while" comes to mind.

    Well, yes. I should've included while and the like. But you can't use a FF
    in "foo = /flip/ .. /flop/":

    irb(main):013:0> foo = /flip/ .. /flop/
    ArgumentError: bad value for range
    from (irb):13

    So ".." and "..." behave differently in a context like "if", "unless",
    "while" etc.

    Hope that cleared it up.

    robert

    Robert Guest

Similar Threads

  1. Replies: 37
    Last Post: February 8th, 02:18 AM
  2. flip flop operator and assignment
    By Phil in forum Ruby
    Replies: 15
    Last Post: February 4th, 09:25 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