Ruby-dev summary 19437-19455

Hi all,

This is a summary of ruby-dev ML in these days.

[ruby-dev:19441] Integer#gcd

Masaki requested a new method Integer#gcd.
Shugo Maeda suggested alternative name, Math.gcd(m,n).

[ruby-dev:19449] new keyword once

See following simple search programs:

#1
ARGF.each do |line|
  print line if line.index('PATTERN')
end

#2
ARGF.each do |line|
  print line if line.index(/PATTERN/)
end

In most cases, Program #1 is SLOWER than #2. The reason is that
a string literal creates a new object for each iteration. So the
following program is faster than #1.

#3
pattern = 'PATTERN'
ARGF.each do |line|
  print line if line.index(pattern)
end

K.Kosako proposed a new keyword `once’ to resolve such kind of
problems. For example, following program creates a string object
only once (as of program #3).

ARGF.each do |line|
  print line if line.index(once 'PATTERN')
end

Matz considers that the better plan is to make string literal create
an object only once.

– Minero Aoki

Hi all,

This is a summary of ruby-dev ML in these days.

[ruby-dev:19441] Integer#gcd

Masaki requested a new method Integer#gcd.
Shugo Maeda suggested alternative name, Math.gcd(m,n).

With Math#gcd know how to handle bignums and GMP integers?

[ruby-dev:19449] new keyword once

Matz considers that the better plan is to make string literal create
an object only once.

If the string literal creates an object only once, then what happens in
this case?

def foo(x)
return x
end

loop do
str = foo(‘TESTING’)
str.chop
end

Paul

···

On Tue, Feb 04, 2003 at 02:50:32AM +0900, Minero Aoki wrote:

Hi –

···

On Tue, 4 Feb 2003, Minero Aoki wrote:

K.Kosako proposed a new keyword `once’ to resolve such kind of
problems. For example, following program creates a string object
only once (as of program #3).

ARGF.each do |line|
  print line if line.index(once 'PATTERN')
end

Matz considers that the better plan is to make string literal create
an object only once.

Is %L already taken? :slight_smile: If not, maybe something like:

print line if line.index(%L{PATTERN})

would be feasible.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Minero Aoki aamine@loveruby.net writes:

ARGF.each do |line|
  print line if line.index(once 'PATTERN')
end

Matz considers that the better plan is to make string literal create
an object only once.

So long as the string is duped when modified. E.g.

def mystring
  'hi'
end

puts mystring.sub!(/h/, 'H')

=> Hi
puts mystring
=> hi

why not a more general

static

which heap allocates/evaluates statement once in the local scope - ananlogous
to C’s static.

-a

···

On Tue, 4 Feb 2003, Paul Brannan wrote:

[ruby-dev:19449] new keyword once

Matz considers that the better plan is to make string literal create
an object only once.

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Paul Brannan wrote:

[ruby-dev:19441] Integer#gcd

Masaki requested a new method Integer#gcd.
Shugo Maeda suggested alternative name, Math.gcd(m,n).

With Math#gcd know how to handle bignums and GMP integers?

Should it be expected to? I know that the gcd method is listed as a
TODO item in Tomasz Wegrzanowski’s ruby-gmp module.

http://pio.ble.pl/~taw/ruby_gmp.alpha9.tar.bz2

Regards,

Dan

clearer to

def mystring
static ‘hi’
end

puts mystring.sub!(/h/, ‘H’) # → Hi
puts mystring # → Hi

VS

def mystring
‘hi’
end

puts mystring.sub!(/h/, ‘H’) # → Hi
puts mystring # → hi

this applies to more than only String objects.

-a

···

On Tue, 4 Feb 2003, Matt Armstrong wrote:

Minero Aoki aamine@loveruby.net writes:

ARGF.each do |line|
  print line if line.index(once 'PATTERN')
end

Matz considers that the better plan is to make string literal create
an object only once.

So long as the string is duped when modified. E.g.

def mystring
  'hi'
end

puts mystring.sub!(/h/, 'H')

=> Hi
puts mystring
=> hi

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

···

At Tue, 4 Feb 2003 03:42:17 +0900, dblack@candle.superlink.net wrote:

Is %L already taken? :slight_smile: If not, maybe something like:

Yes. Kosako mentioned similar one, but suggested more general
form because it’s String specific.


Nobu Nakada

Hi,

···

In message “Re: ruby-dev summary 19437-19455” on 03/02/04, ahoward ahoward@fsl.noaa.gov writes:

why not a more general

static

which heap allocates/evaluates statement once in the local scope - ananlogous
to C’s static.

C’s static is global that extent is limited to enclosing block (or file).
I don’t think it’s what we want.

						matz.

No, I’m just pointing out that using Math.gcd instead of Integer#gcd
means that I lose the ability to easily write a function that works on
both GMP integers and regular integers:

A function that returns 10 times the gcd of x and 5, for whatever

stupid reason

def foo(x)
case x
when Integer
return 10 * Math.gcd(x, 5)
else
return 10 * x.gcd(5)
end
end

I would much rather write:

def foo(x)
return 10 * x.gcd(5)
end

instead of having to write a special case for Integers.

Paul

···

On Tue, Feb 04, 2003 at 03:56:03AM +0900, Daniel Berger wrote:

Paul Brannan wrote:

[ruby-dev:19441] Integer#gcd

Masaki requested a new method Integer#gcd.
Shugo Maeda suggested alternative name, Math.gcd(m,n).

With Math#gcd know how to handle bignums and GMP integers?

Should it be expected to? I know that the gcd method is listed as a
TODO item in Tomasz Wegrzanowski’s ruby-gmp module.

Hi,

[ruby-dev:19449] new keyword once

Matz considers that the better plan is to make string literal create
an object only once.

why not a more general

static

which heap allocates/evaluates statement once in the local scope - ananlogous
to C’s static.

This proposal doesn’t mean what you want. The value evaluated
with `once’ can’t be varied.

I guess your `method static’ variable can be achieved as:

def foo
method_static = once [0]
puts “called #{method_static[0]} times”
end

···

At Tue, 4 Feb 2003 03:14:32 +0900, ahoward wrote:


Nobu Nakada

not

static
int
method ()
{
return 42;
}

rather

static
int
method ()
{
static int count = 0; // ← THIS STATIC
printf (“method() has been called %d times\n”, count++);
return 42;
}

ruby static → evaluated once, and once only, in the enclosing scope where
‘enclosing scope’ is subject to modification as per your ‘block local’ rules
and where they are headed.

-a

···

On Tue, 4 Feb 2003, Yukihiro Matsumoto wrote:

Hi,

In message “Re: ruby-dev summary 19437-19455” > on 03/02/04, ahoward ahoward@fsl.noaa.gov writes:

why not a more general

static

which heap allocates/evaluates statement once in the local scope - ananlogous
to C’s static.

C’s static is global that extent is limited to enclosing block (or file).
I don’t think it’s what we want.

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

In C (and C99 and C++), the static keyword has different meanings,
depending on the context:

// tentative declaration of x; x will have internal linkage (no symbol
// is generated for x, thus x is not visible outside this translation
// unit)
static int x;

// definition of x; it will have value 42 when initialized
static int x = 42;

void foo() {
// x is a local variable but has static storage duration (that is,
// its lifetime is as long as the program, and it is initialized
// when the program starts). It is not global; this is a
// function-local variable that shadows a translation-unit-local
// variable.
static int x = 10;
}

// C++: example of a static member function; indicates that the function
// has no implicit this argument (and can therefore be called without an
// object); this is analgous to a class method in Ruby.
class Foo {
static void foo();
}

void Foo::foo()
{
// C++: example of a variable with static storage duration; its
// lifetime begins when the below statement is executed. (**)
static Bar b;
}

// C99: the argument corresponding to a in any call to f must be a
// non-null pointer to the first of at least three arrays of 5
// doubles.
void f(double a[restrict static 3][5]);

In short, the static keyword in C is very surprising. If we were to add
it to Ruby, we must be certain that there is no ambiguity and that there
is no surprise involved, as that would violate POLS. Of course, if Ruby
had a static keyword and it did not have a few surprises up its sleeve,
I would be very surpised, and that too could potentially violate POLS.
:slight_smile:

FWIW, I think what the author intended was the usage of static that I
have marked with (**).

Paul

···

On Tue, Feb 04, 2003 at 03:35:50AM +0900, Yukihiro Matsumoto wrote:

In message “Re: ruby-dev summary 19437-19455” > on 03/02/04, ahoward ahoward@fsl.noaa.gov writes:

why not a more general

static

which heap allocates/evaluates statement once in the local scope - ananlogous
to C’s static.

C’s static is global that extent is limited to enclosing block (or file).
I don’t think it’s what we want.

Hi,

···

In message “Re: ruby-dev summary 19437-19455” on 03/02/04, ahoward ahoward@fsl.noaa.gov writes:

C’s static is global that extent is limited to enclosing block (or file).
I don’t think it’s what we want.

 static int count = 0;				      // <-- THIS STATIC

I know. It’s still global with constant initializer.

						matz.

actually i meant the

void
foo ()
{
// x is a local variable but has static storage duration (that is,
// its lifetime is as long as the program, and it is initialized
// when the program starts). It is not global; this is a
// function-local variable that shadows a
// translation-unit-local
// variable.
static int x = 10;
}

example.

this is the usage i would advocate ruby.

thanks for the elaboration.

see other post.

-a

···

On Tue, 4 Feb 2003, Paul Brannan wrote:

FWIW, I think what the author intended was the usage of static that I
have marked with (**).

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

i’m not exactly sure how? my understanding is from the K&R book. if what you
are saying is true, i’m not sure exactly how this works :

~/eg/c > cat static.c

#include <stdlib.h>
#include <stdio.h>

void
foo (void)
{
static int count = 0;
printf (“%s called %d times\n”, FUNCTION, count++);
}

void
bar (void)
{
static int count = 0;
printf (“%s called %d times\n”, FUNCTION, count++);
}

~/eg/c > cat main.c

extern void foo (void);
extern void bar (void);

int
main (argc, argv, env)
int argc;
char **argv;
char **env;
{
int i;

for (i = 0; i < 4; i++)
  {
foo ();
bar ();
  }
return 0;

}

~/eg/c > gcc main.c static.c && a.out

foo called 0 times
bar called 0 times
foo called 1 times
bar called 1 times
foo called 2 times
bar called 2 times
foo called 3 times
bar called 3 times

this looks like the two counts are made ‘private’ to foo and bar, and also
that the scope is not global, although i realize that allocation in the data
segment might be what you meant by ‘global’. i was referring to their scopes,
which certainly seem not to be global. i have a feeling you will insert a
reason that i am wrong,

here →

in any case, a ruby ‘static’ could be similar in meaning if not in
implimentation. perhaps this is more difficult than i realize to impl.

in any case it seems like it would be usefull to have this ability if not
the syntax (no one ever likes my syntax ideas :wink: ).

-a

···

On Tue, 4 Feb 2003, Yukihiro Matsumoto wrote:

Hi,

In message “Re: ruby-dev summary 19437-19455” > on 03/02/04, ahoward ahoward@fsl.noaa.gov writes:

C’s static is global that extent is limited to enclosing block (or file).
I don’t think it’s what we want.

 static int count = 0;				      // <-- THIS STATIC

I know. It’s still global with constant initializer.

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

 static int count = 0;				      // <-- THIS STATIC

I know. It’s still global with constant initializer.

i’m not exactly sure how? my understanding is from the K&R book. if what you
are saying is true, i’m not sure exactly how this works :

I wish I could explain better.

The scope of “static variables” are its enclosing block, but it is a
“global variable” that can only be seen from the block. You know I
mean? It’s not a variable that initialized once. From that reason,
“static” is not a proper name for new keyword.

in any case it seems like it would be usefull to have this ability if not
the syntax (no one ever likes my syntax ideas :wink: ).

Maybe. But since there’s thread synchronization issue, things are
more difficult that they seem.

						matz.
···

In message “Re: ruby-dev summary 19437-19455” on 03/02/04, ahoward ahoward@fsl.noaa.gov writes: