Avi Bryant | 1 Feb 2004 01:09

Re: Save the flip/flop op!

ptkwt <at> aracnet.com (Phil Tomson) wrote in message news:<bvgv9u2mb1 <at> enews1.newsguy.com>...

> And just what would you replace that functionality with?  It seems to me 
> that it would take several lines of code to replace the following 
> functionality:
> 
>   f.each_line {|l|
>     if l=~/BEGIN/ .. l=~/END/
>       #do something in this range of lines
>     end
>   }

What an odd operator!  It took me a bit of toying around to figure out
exactly what it does (I didn't know what the scope of its state
variable was, but evidently it's the enclosing method).

Anyway, it seems to me you could pretty easily replace this with a
simple flipflop class:

ff = FlipFlop.new
f.each_line {|l|
  ff.from(l=~/BEGIN/, l=~/END/) do
       #do something in this range of lines
  end
}

class FlipFlop
  def initialize
      <at> in_range = false
  end
(Continue reading)

Avi Bryant | 1 Feb 2004 01:14

Re: Save the flip/flop op!

I wrote:

  def from(start_cond, end_cond)
       <at> in_range = true if start_cond
      yield
       <at> in_range = false if end_cond
   end

Ack.  That should be "yield if  <at> in_range", of course.

Phil Tomson | 1 Feb 2004 01:54

Re: Save the flip/flop op!

In article <6e869a6b.0401311614.1334a5fc <at> posting.google.com>,
Avi Bryant <avi <at> beta4.com> wrote:
>I wrote:
>
>  def from(start_cond, end_cond)
>       <at> in_range = true if start_cond
>      yield
>       <at> in_range = false if end_cond
>   end
>
>Ack.  That should be "yield if  <at> in_range", of course.

Seems to work and be nestable, as in the usage here:

ff = FlipFlop.new
ff2= FlipFlop.new
f = File.open('filename')
f.each_line {|l|
  ff.from(l=~/BEGIN/, l=~/END/) do
     #inside the BEGIN .. END block
     ff2.from(l=~/def/,l=~/end/) do
       #inside def .. end block
       puts l
     end
  end
}

But it's more cumbersome having to predeclare all the FlipFlops.

Phil
(Continue reading)

Michael campbell | 1 Feb 2004 02:05
Picon
Favicon

Re: Save the flip/flop op!

Phil Tomson wrote:

> In article <6e869a6b.0401311614.1334a5fc <at> posting.google.com>,
> Avi Bryant <avi <at> beta4.com> wrote:
> 
>>I wrote:
>>
>> def from(start_cond, end_cond)
>>      <at> in_range = true if start_cond
>>     yield
>>      <at> in_range = false if end_cond
>>  end
>>
>>Ack.  That should be "yield if  <at> in_range", of course.
> 
> 
> 
> Seems to work and be nestable, as in the usage here:
> 
> ff = FlipFlop.new
> ff2= FlipFlop.new
> f = File.open('filename')
> f.each_line {|l|
>   ff.from(l=~/BEGIN/, l=~/END/) do
>      #inside the BEGIN .. END block
>      ff2.from(l=~/def/,l=~/end/) do
>        #inside def .. end block
>        puts l
>      end
>   end
(Continue reading)

Phil Tomson | 1 Feb 2004 03:09

Re: Save the flip/flop op!

In article <401C50D5.601 <at> yahoo.com>,
Michael campbell  <michael_s_campbell <at> yahoo.com> wrote:
>Phil Tomson wrote:
>
>> In article <6e869a6b.0401311614.1334a5fc <at> posting.google.com>,
>> Avi Bryant <avi <at> beta4.com> wrote:
>> 
>>>I wrote:
>>>
>>> def from(start_cond, end_cond)
>>>      <at> in_range = true if start_cond
>>>     yield
>>>      <at> in_range = false if end_cond
>>>  end
>>>
>>>Ack.  That should be "yield if  <at> in_range", of course.
>> 
>> 
>> 
>> Seems to work and be nestable, as in the usage here:
>> 
>> ff = FlipFlop.new
>> ff2= FlipFlop.new
>> f = File.open('filename')
>> f.each_line {|l|
>>   ff.from(l=~/BEGIN/, l=~/END/) do
>>      #inside the BEGIN .. END block
>>      ff2.from(l=~/def/,l=~/end/) do
>>        #inside def .. end block
>>        puts l
(Continue reading)

Zach Dennis | 1 Feb 2004 03:29

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

Avi Bryant | 1 Feb 2004 04:35

Re: Save the flip/flop op!

ptkwt <at> aracnet.com (Phil Tomson) wrote:

> Seems to work and be nestable, as in the usage here:

> ff = FlipFlop.new
> ff2= FlipFlop.new
> f = File.open('filename')
> f.each_line {|l|
>  ff.from(l=~/BEGIN/, l=~/END/) do
>    #inside the BEGIN .. END block
>     ff2.from(l=~/def/,l=~/end/) do
>       #inside def .. end block
>       puts l
>     end
>  end
> }

> But it's more cumbersome having to predeclare all the FlipFlops.

Now that raises an interesting point.  Say you had this input:

---
BEGIN
def baz 
#oops, syntax error
END

BEGIN
#we shouldn't see this line
END
(Continue reading)

Daniel Berger | 1 Feb 2004 04:39
Picon
Favicon

Re: Ruby 1.8.1 REXML performance

Steven Jenkins <steven.jenkins <at> ieee.org> wrote in message news:<401C268F.80805 <at> ieee.org>...
> Quick update--I'll post numbers later. It appears that the REXML stream 
> parser in Ruby 1.8.1 is worse than O(n) in the number of input lines (or 
> tags). Ruby 1.8.0 appears to be pretty close to O(n) (as it should).
> 
> I hope soon I won't be the only person in this thread :-).
> 
> Steve

Steve,

One thing you can do is run your code through the profiler (ruby -r
profile yourcode.rb) and show us the results.  This could help us
identify a potential bottleneck in REXML and/or Ruby itself.

Regards,

Dan

John W. Kennedy | 1 Feb 2004 05:19

Re: Save the flip/flop op!

Hal Fulton wrote:
> I meant it was ugly and obtuse, and no one would ever think otherwise
> unless he was used to Perl.

Code with an invisible state always reminds me of COBOL, frankly.

--

-- 
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
   -- Charles Williams.  "Judgement at Chelmsford"

Gavin Sinclair | 1 Feb 2004 06:04
Picon

Constant visibility

Folks,

I know this sort of thing has been talked about before, but that's not
going to stop me :)

  module M
    X = 5
    class C
      def foo
        puts "X = #{X}"
      end
    end
  end

  class M::C
    def bar
      puts "X = #{X}"
    end
  end

  M::C.new.foo            # X = 5
  M::C.new.bar            # NameError: uninitialized constant M::C::X

The problem is that when you use 'class M::C' to open a class, you
can't access constants declared in M, whereas if you use 'module M;
class C', you can.

1) Why is this?

2) Is is an intentional behaviour of Ruby?
(Continue reading)


Gmane