No way of looking for a regrexp match starting from a particular point in a string?

Is $~ thread safe?

To bad it has to be done this way (though my library will hide it). I first looked at Ruby several years ago, and at that time, didn't go further with it because it was too PERLish for me. (PERL was great for its time, but speaking as someone who actually had to maintain a lot of PERL code, it's actually a pretty grotty language). One of the things that brought me back to Ruby was the fact that an effort was being made to move Ruby away from its PERLisms. But I guess it'll take a while longer...

Thanks everyone,
Ken

Rick DeNatale wrote:

···

On 6/3/07, Devin Mullins <twifkak@comcast.net> wrote:

Nobuyoshi Nakada wrote:
> String#index also sets $~.
For that matter, so does String#scan.

Hence:
irb(main):001:0> "abcdefabc".scan(/abc/) {puts "#{$~.inspect}, #{$~}"}
#<MatchData:0xb7b0220c>, abc
#<MatchData:0xb7b021e4>, abc
=> "abcdefabc"

Copying in this case is not caused by using the string as a parameter but by appending to it.

I thought this thread was about /scanning/ which is a read only operation. Did I miss something?

Kind regards

  robert

···

On 04.06.2007 14:06, Robert Dober wrote:

On 6/4/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 04.06.2007 13:28, Robert Dober wrote:

Robert, actually string[n..-1] is cheaper than you might assume: I
believe the new string shares the char buffer with the old string, so
you basically just get a new String object with a different offset - the
large bit (the char data) is not copied.

I am afraid that this is not true anymore when the slice is passed as
a formal parameter, the data has to be copied :frowning:

irb(main):011:0> def change(x)
irb(main):012:1> x << "changed"
irb(main):013:1> end
=> nil
irb(main):014:0> a="abcdef"
=> "abcdef"
irb(main):015:0> change(a[1..2])
=> "bcchanged"
irb(main):016:0> a
=> "abcdef"

Kenneth McDonald wrote:

Is $~ thread safe?

Yes. All the regex match "global" variables are actually per-thread. See p.319 of Pick Axe 2nd ed.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Is $~ thread safe?

Yes.

To bad it has to be done this way (though my library will hide it). I first looked at Ruby several years ago, and at that time, didn't go further with it because it was too PERLish for me. (PERL was great for its time, but speaking as someone who actually had to maintain a lot of PERL code, it's actually a pretty grotty language). One of the things that brought me back to Ruby was the fact that an effort was being made to move Ruby away from its PERLisms. But I guess it'll take a while longer...

Thanks everyone,

Ken, I still don't understand why exactly you need MatchData objects. What are you trying to achieve?

Kind regards

  robert

···

On 04.06.2007 00:44, Kenneth McDonald wrote:

Hi --

···

On Mon, 4 Jun 2007, Kenneth McDonald wrote:

Is $~ thread safe?

To bad it has to be done this way (though my library will hide it). I first looked at Ruby several years ago, and at that time, didn't go further with it because it was too PERLish for me. (PERL was great for its time, but speaking as someone who actually had to maintain a lot of PERL code, it's actually a pretty grotty language). One of the things that brought me back to Ruby was the fact that an effort was being made to move Ruby away from its PERLisms. But I guess it'll take a while longer...

The best thing is really just to use Ruby without thinking about Perl.
They're very different languages, and get mentioned in the same breath
far too often.

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

>
>> Robert, actually string[n..-1] is cheaper than you might assume: I
>> believe the new string shares the char buffer with the old string, so
>> you basically just get a new String object with a different offset - the
>> large bit (the char data) is not copied.
> I am afraid that this is not true anymore when the slice is passed as
> a formal parameter, the data has to be copied :frowning:
>
> irb(main):011:0> def change(x)
> irb(main):012:1> x << "changed"
> irb(main):013:1> end
> => nil
> irb(main):014:0> a="abcdef"
> => "abcdef"
> irb(main):015:0> change(a[1..2])
> => "bcchanged"
> irb(main):016:0> a
> => "abcdef"

Copying in this case is not caused by using the string as a parameter
but by appending to it.

I thought this thread was about /scanning/ which is a read only
operation. Did I miss something?

No you did not, theoretically it might work like this:

def change( x )
   x << changed # copy on write
end

a="some string"
b=a[1..3] # shallow copy
b << "changed" # copy on write
a << "changed" # no copy of course

but do you think it does? Note that the object must have state to know
when and how to copy the underlying data, I am about to read string.c
but it is quite complicated and I got some work to do :(.

Cheers
Robert

···

On 6/4/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 04.06.2007 14:06, Robert Dober wrote:
> On 6/4/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 04.06.2007 13:28, Robert Dober wrote:

Kind regards

        robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw