anything disappearing from Ruby for 2.0?

A

Ara.T.Howard

Date: Mon, 1 Dec 2003 23:49:32 +0900
From: Yukihiro Matsumoto <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: anything disappearing from Ruby for 2.0?

Hi,

In message "Re: anything disappearing from Ruby for 2.0?"

|Hi :) Here's another idea:
|
|1) Make #.. a method (Integer#.. creates ranges, for example)
|
|2) Make a shortcut to proc {...} (maybe "&{"? Anything, I don't know)
|
|3) Define Proc#.. to work as a flip-flop
|
|4) while &{ line =~ /abc/ }..&{ line =~ /def/ }
|
|Not as easy as before, but almost...
|
|What do you think?

I like the basic idea. the "shortcut notation" would be a key. I
couldn't think of a good one for years. But someone might be able to.

matz.

how about extending readlines and defining an IO#[] method:

class IO
def readlines range = nil
...
end
def [] fixnum_or_range_of_fixnum_or_regexp
...
end
end


lines = IO.readlines /abc/ .. /def/

open(path) do |f|
lines = f[ /abc/ .. /def/ ]
end


#[] could also work as a 'seek and read' type operator.

record = f[offset, length]

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
T

T. Onoma

I like the basic idea. the "shortcut notation" would be a key. I
couldn't think of a good one for years. But someone might be able to.

If I understand correctly,

In ruby-core:1792, Mathieu Bouchard hints at:

{ || ... } -or- { |*| ... }

Could something like that work? Or would it be too ambiguious with blocks?

def pshooter(a)
a.call
yield if block_given?
end

pcatch( {|| puts "p" } ) { puts "block" }

#> p
#> block


BTW: why does this happen?

a = proc { puts "p" }
pshooter a # => p
pshooter a { puts "block" }
# => undefined method `a' for main:Object (NoMethodError)


T
 
C

Carlos

BTW: why does this happen?
a = proc { puts "p" }
pshooter a # => p
pshooter a { puts "block" }
# => undefined method `a' for main:Object (NoMethodError)

The block associates with 'a': pshooter(a { puts "block"}), so ruby
interprets 'a' as a method. In this case you should use do ... end, it is
more "loose".
 
T

T. Onoma

The block associates with 'a': pshooter(a { puts "block"}), so ruby
interprets 'a' as a method. In this case you should use do ... end, it is
more "loose".

ah! right, right. Thanks.
 
D

Dave Brown

: > |
: > |if (/foo/ .. /bar/) ?
: > |
: > |I find this construct terribly useful; I'd hate to see it go.
: >
: > Perhaps some other way to accomplish, for example,
: >
: > IO#from_to(re1, re2) {|line| ... }
: >
: > just an idea.
:
: Hi :) Here's another idea:
:
: 1) Make #.. a method (Integer#.. creates ranges, for example)
:
: 2) Make a shortcut to proc {...} (maybe "&{"? Anything, I don't know)
:
: 3) Define Proc#.. to work as a flip-flop
:
: 4) while &{ line =~ /abc/ }..&{ line =~ /def/ }
:
: Not as easy as before, but almost...
:
: What do you think?

An operator .. would be nice.

Lacking that, though, here's something I threw together in ten minutes:

class FlipFlop
class << self
@@flipflopvalue = false # not thread safe, I imagine
def flipflopon
@@flipflopvalue = true
end
def flipflopoff
@@flipflopvalue = false
end
def flipflopval
@@flipflopvalue
end
end
end

class Object # Everything is true except false and nil...right?
def until(rhs) FlipFlop.flipflopon end
end

module FalseFlipFlop
def until(rhs)
if (rhs) then FlipFlop.flipflopoff end
FlipFlop.flipflopval
end
end

class FalseClass
include FalseFlipFlop
end

class NilClass
include FalseFlipFlop
end

(The ability to do this kind of thing is what makes me say "Wow,
what a great language Ruby is!" a lot.)

I tested it with this:

$ ls|ruby -rflipflop -ne 'print if ($_ =~ /^b/).until($_ =~ /^d/)'

And it worked as I'd hoped it would. Unfortunately I couldn't
define a method called "..". :)

--Dave
 
M

Martin DeMello

Yukihiro Matsumoto said:
I like the basic idea. the "shortcut notation" would be a key. I
couldn't think of a good one for years. But someone might be able to.

\{...} perhaps, by analogy with Haskell's \ for lambda.

martin
 
H

Hal Fulton

Martin said:
\{...} perhaps, by analogy with Haskell's \ for lambda.

Backslash is always problematic since it's used as an escape.

I really like the &{...} idea, for the following reasons:

1. It's parallel in some sense to the &block notation, much as
* can be used on formal or actual parameters.

2. & is already a recognized operator (token).

3. There's no unary & in use yet except &block.

But maybe there is a good reason this should not be used?

Another note: I assume it would work with do/end also:

myproc = & do blah; blah end

which I might even write as

myproc = &do blah; blah end



Hal
 
J

Josef 'Jupp' SCHUGT

Hi!

* David Garamond; 2003-12-01, 12:43 UTC:
I find the ability to write one-liners _amazingly_ useful. Perhaps
you don't use shell/command-line interface much.

If propose one-lines you possibly did not yet discover the *true*
strength of using a CLI. A few years ago I would have supported your
statement. But then I started to use shell multiliners during
interactive sessions.
But many, many Unix/Linux users do most of their stuffs in shell
daily. And using a combination of Unix commands sometimes are just
not powerful/easy/portable enough. I use Perl & Ruby one-liners
for all things, from renaming a bunch of files according to some
string/regex operation (e.g. changing all ".tar.gz" to ".tgz"),

That can easily be done using the following (KSH, Bash should do as
well) three-liner:

for i in *.tar.gz; do
mv $i ${i%.tar.gz}.tgz
done

One way of achieving the same in Ruby is the following five-liner:

ruby <<EOF
%x(ls *\.tar\.gz).each { |x|
x.chomp!
%x(mv #{x} #{x.sub(/\.tar\.gz$/, '.tgz')})
}
EOF

You could also use 'echo' in place of 'ls' but then you would have to
replace 'each' by 'split.each'. Of course You can also do the same in
100% Ruby but I don't see why I should use a scripting language if
the shell allows me to do it more easily so I didn't take the time
for getting rid of ls and mv.
Btw, I even have a text file where I store hundreds of Perl & Ruby
one-liners which I can just copy paste to the shell window whenever
I needs them. It beats having hundreds of script files which I
don't know how to name properly.

The same works for multiliners as well.

I don't see why to make everything fit into one line if there is no
necessity for it.

Josef 'Jupp' Schugt
 
J

Josef 'Jupp' SCHUGT

Hi!

* Yukihiro Matsumoto; 2003-12-01, 21:55 UTC:
[candidates for deletion]
Actually, it's Precision module. Don't ask me what it is.

This is what the rubydoc documentation has on it (I did en passent
correct some nihonisms like singular/plural issues and 'k' in place
of 'c' to make it more readable):
Precision is a Mix-in for concrete numerical classes with
precision. Here, `precision' means the fineness of approximation
of a real number, so, this module should not be included into
non-subset of real, e.g. abstract numerical, complex, matrix.

induced_from(number)

Creates an object which is converted from number. Since it by
default raises TypeError, redefine before use. Note that a use of
prec in a redefinition may causes an infinite loop.

prec(class)

Converts self into an instance of class. By default, prec invokes
class.induced_from(self) and returns its value. So, if
class.induced_from doesn't correspond to the class of self, it is
neccessary to redefine this prec.

prec_i

Returns an integer converted from self. It is equivalent to prec
(Integer).

prec_f

Returns a floating-point number converted from self. It is
equivalent to prec(Float).

According to me there is good reason not to remove that. In natural
sciences knowing the precision of values is as important as knowing
the values themselves (if not more important). To give an example:
The result of a computation is that a particle that according to
theory should have a rest mass of 0 has a rest mass of 1 eV(*). If
the imprecision of that value is 0.1 eV the result is strong evidence
that theory is wrong or that some mistake was made interpreting the
experiment. If the uncertainty is 2 eV the same value is in accord
with theory. The imprecision is partly introduced by uncertainties in
measurements but also by imprecisions of numerical representations of
real values.

(*) Precisely taken 1 eV is a mass energy equivalent (because of
Einstein's E=mc^2 it makes perfect sense to express mass in terms
of energy, the difference is just some poportionality factor...)

Josef 'Jupp' Schugt
 
B

Bill Kelly

From: "Josef 'Jupp' SCHUGT said:
for i in *.tar.gz; do
mv $i ${i%.tar.gz}.tgz
done

One way of achieving the same in Ruby is the following five-liner:

ruby <<EOF
%x(ls *\.tar\.gz).each { |x|
x.chomp!
%x(mv #{x} #{x.sub(/\.tar\.gz$/, '.tgz')})
}
EOF

Your shell solution is nice. My Ruby would have been:

ruby -e "ARGV.each{|f| `mv #{f} #{f.sub(/\.tar\.gz$/,'.tgz')}`}" *.tar.gz

...or, less likely (but perhaps on Windows):

ruby -rftools -e "ARGV.each{|f| File.mv(f, f.sub(/\.tar\.gz$/,'.tgz'))}" *.tar.gz
You could also use 'echo' in place of 'ls' but then you would have to
replace 'each' by 'split.each'. Of course You can also do the same in
100% Ruby but I don't see why I should use a scripting language if
the shell allows me to do it more easily so I didn't take the time
for getting rid of ls and mv.

You seem to be saying if it can be done interactively using shell
commands, then good. But if it can be done easily in Ruby in a
one-liner, then bad.

But I like both. I'm an avid fan of Un*x shell commands... Many
of my Ruby one-liners are strung together in pipes between the
likes of find and grep, etc.

But I've found Ruby & Perl's "one-lining" ability very freeing.
What used to make my brain explode in corruscations of cut and
sort and awk and sed, I can now often approach more simply [to me]
in Perl or Ruby. I'd prefer Ruby.

. . For instance, I wanted to find out which entities were used
in a group of HTML files, and how many times each entity occurred.
(Like, &ndash; is used twice, &mdash; isn't used at all, &aacute;
appears 7 times, etc...)

In Ruby, I just wrote:

ruby -ne "h||=Hash.new(0); scan(/&#?\w+;/){|e| h[e]+=1}; END{p h}" *.html

..Cool! Problem solved!

...Now I suppose there *might* be some way to get a comparable result
with sed & sort & grep, maybe, ... I'm not sure, though. Whatever it is,
I suspect it would be annoying, compared to the Ruby. :)


Regards,

Bill
 
N

nobu.nokada

Hi,

At Mon, 1 Dec 2003 23:36:25 +0900,
Carlos said:
Hi :) Here's another idea:

1) Make #.. a method (Integer#.. creates ranges, for example)

2) Make a shortcut to proc {...} (maybe "&{"? Anything, I don't know)

3) Define Proc#.. to work as a flip-flop

4) while &{ line =~ /abc/ }..&{ line =~ /def/ }

This needs a hidden flip-flop variable, otherwise matching
/abc/ will be performed each time.

Of course, you can do it like this, but, hmmmm...

(&{ line =~ /abc/ }..&{ line =~ /def/ }).while do
# ...
end
 
D

David Garamond

Josef said:
The same works for multiliners as well.

I don't see why to make everything fit into one line if there is no
necessity for it.

Many of my "one-liners" actually span several physical lines :) (Too
bad CMD.EXE doesn't seem to allow newline characters in one command
line, or does it?).
 
E

Emmanuel Touzery

Yukihiro said:
Hi,

In message "Re: anything disappearing from Ruby for 2.0?"

|> * Perl style variables
|
|Just removal of $#{some_character} or also removal of
|${Insert_some_alias_from_English_dot_rb} ?

Both. But I have consider about each variable before removing. Some
of them are useful.
i don't know if $* is in this list, but i consider it extremely
intuitive to remember (also because it's used in shell scripts AFAIK)
and also concise. actually i don't know the alternative and i don't want
to wonder if it's all caps, with underscores or whatever...

i hope this one will stay.

emmanuel
 
R

Robert Klemme

David Garamond said:
Many of my "one-liners" actually span several physical lines :) (Too
bad CMD.EXE doesn't seem to allow newline characters in one command
line, or does it?).

It does - just the escape character is a bit different. But you didn't
seriously expect it to be "\" on a MS system, did you? ;-)

File foo.bat:
echo ^
test ^
case

This outputs "test case".

Cheers

robert
 
C

Carlos

4) while &{ line =~ /abc/ }..&{ line =~ /def/ }
This needs a hidden flip-flop variable, otherwise matching
/abc/ will be performed each time.

Yes, because a new Proc is created every time. I didn't notice that :(. I
should have thought, if proc{...}==Proc.*new*{...}, that would be the
case :). Sorry.
Of course, you can do it like this, but, hmmmm...

(&{ line =~ /abc/ }..&{ line =~ /def/ }).while do
# ...
end

And also, the flip-flop is mostly used with "if" inside an external loop.
I think, if we want to keep this useful construction, must find another
way...
 
T

T. Onoma

If I understand correctly,

In ruby-core:1792, Mathieu Bouchard hints at:

{ || ... } -or- { |*| ... }

Could something like that work? Or would it be too ambiguious with blocks?

From what I understand, the problem with the above shorthand for procs is
this:

def a(x=nil)
x.call if a('call')
yield('block') if block_given?
end

a {|z| puts "which? #{z}"}

It occured to me today that it's rather odd that blocks can be passed to any
method whether the method uses the block or not. --a method need not
explictly state it takes a block, as it does with everyhting else. It really
is odd if you think about it. What if you could do that with any argument?
Imagine having arguments always passed in to a special keyword, args, and
using parameters_given? :) LoL! Cracks me up! I guess I shouldn't laugh too
much though. In fact having a keyword like args anyway could cetainly be
useful (since we can always count on the name). Imagine:

def a(*)
puts args.join(', ')
end

Well, I suppose it's a little too late to rethink explict acceptance of block
parameters now. So then. Is there any other way to resolve the above problem?
Short of a YAP (Yet Another Perlism), the general census hasn't been able to
provded one. But there is one possibility which, seems to me, might actually
work just fine: let blocks take precedence over lambda parameters. A clsely
related concern is already an appearent consideration given the binding rules
the differentiate {...} and do..end. And it makes sense since you can always
unambiguate it with parenthesis when need be:

a( {|z| puts "which? #{z}"} )

It wouldn't break old code either since currently the keyword method proc or
lambda is used in the cases in which it otherwise could, eg.

a proc { |z| ... }

So if we want a shorthand for lambdas, the already used concise notation of
{ || ... } in fact does offer a good solution, and weighs in to stave-off Yet
Another Perlism.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top