Beautiful Code : Pity he didn't ask here...

A

ara.t.howard

I meant that I believe it was Ara who recommended it as a good name
for an unused block parameter in Ruby.

i'll only take credit if beer is involved ... ;-)


-a
 
A

ara.t.howard

Gosh, I think I totally disagree on readability grounds.

let's step back for one moment and think about why we all left perl:

- wtf is $1 ?
- wtf is ARGF ?
- regexes are, of all things, the epitome of snoopy swearing

these are the kinds of magic things that used to send us all
perdoc'ing to read our own code.

while i use both of those thing in dirty script i keep in ~/bin/ i'd
never publish them as beautiful. names are the most important thing
in programing and terminals are pretty dang wide these days

given that, why not

pattern =
%r|GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+)|x

STDIN.each_line do |line|
match, date, *ignored = pattern.match(line).to_a
next unless date
puts date
end

i always consider using the magic $vars a sign of temporary perl
madness.

kind regards.

-a
 
M

Marcel Molina Jr.

%r|GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+)|x

Looking at all those \d is kind of hard to parse also. I think the following
way of expression that pattern is considerably clearer:

%r|GET /ongoing/When/\d{3}x/(\d{4}/\d{2}/\d{2}/[^ .]+)|x

Readability improves even more when Oniguruma allows for named captures.

marcel
 
J

James Edward Gray II

let's step back for one moment and think about why we all left perl:

- wtf is $1 ?
- wtf is ARGF ?
- regexes are, of all things, the epitome of snoopy swearing

You just insulted all of my best friends in one go. I think I better =20=

get my butt back to Perl=85 :)

James Edward Gray II=
 
C

Chad Perrin

let's step back for one moment and think about why we all left perl:

Speak for yourself. I didn't leave Perl at all. I just added Ruby to my
repertoire. I'm not sold on the idea that there's One True Language, and
its name is Ruby. I use different languages for different purposes.
Some languages are generally good, and some are generally bad, but many
languages are good for at least one thing in a way that other languages
aren't -- and for that reason among others, I use both Perl and Ruby.

- wtf is $1 ?
- wtf is ARGF ?
- regexes are, of all things, the epitome of snoopy swearing

$1 is the first capture. Seems obvious to me.
ARGF is closely related to ARGV.
Regexen are extremely useful, and removing them from Ruby would be a bit
like shooting it in the head.

i always consider using the magic $vars a sign of temporary perl
madness.

I always see it as a potential sign of greater maintainability (if
they're used well).
 
A

ara.t.howard

Looking at all those \d is kind of hard to parse also. I think the
following
way of expression that pattern is considerably clearer:

%r|GET /ongoing/When/\d{3}x/(\d{4}/\d{2}/\d{2}/[^ .]+)|x

Readability improves even more when Oniguruma allows for named
captures.

yeah, i actually like to break them out like

%r|
GET
\d\d\d\d # year
\d\d # month
|

but i didn't want to go overboard!

named captures are a real want in ruby right now if you ask me. i've
been using the idiom

match, yyyy, mm, dd, *ignored = pattern.match(string).to_a

if match
...
end

as a workaround

(for those that don't know the captures in patterns can be got that way)

cheers.

-a
 
A

ara.t.howard

let's step back for one moment and think about why we all left perl:

Speak for yourself. I didn't leave Perl at all. I just added Ruby
to my
repertoire. I'm not sold on the idea that there's One True
Language, and
its name is Ruby. I use different languages for different purposes.
Some languages are generally good, and some are generally bad, but
many
languages are good for at least one thing in a way that other
languages
aren't -- and for that reason among others, I use both Perl and Ruby.

- wtf is $1 ?
- wtf is ARGF ?
- regexes are, of all things, the epitome of snoopy swearing

$1 is the first capture. Seems obvious to me.
ARGF is closely related to ARGV.
Regexen are extremely useful, and removing them from Ruby would be
a bit
like shooting it in the head.

i always consider using the magic $vars a sign of temporary perl
madness.

I always see it as a potential sign of greater maintainability (if
they're used well).

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
John W. Russell: "People point. Sometimes that's just easier. They
also use
words. Sometimes that's just easier. For the same reasons that
pointing has
not made words obsolete, there will always be command lines."


hey chad-

i've been writing oo-perl for many moons, setting $/ to slurp files
and all that... nevertheless the are features of ruby that attracted
to me it from the context of perl and continue to stand out when
selecting one of the two languages for a particular purpose. the
power of ruby is that of abstraction and cleanliness.

ps. truth be told i really only maintain perl now and haven't
written anything new in it for years. ;-)

-a
 
J

John Joyce

%r|GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/[^ .]+)|x

Looking at all those \d is kind of hard to parse also. I think the
following
way of expression that pattern is considerably clearer:

%r|GET /ongoing/When/\d{3}x/(\d{4}/\d{2}/\d{2}/[^ .]+)|x

Readability improves even more when Oniguruma allows for named
captures.

marcel
It is hard to parse. That's why all RegExes should include a comment
describing wtf it is supposed to match!
Ideally, with an example string.
Oh, wait, that's halfway to a test unit!
 
C

Chad Perrin

i've been writing oo-perl for many moons, setting $/ to slurp files
and all that... nevertheless the are features of ruby that attracted
to me it from the context of perl and continue to stand out when
selecting one of the two languages for a particular purpose. the
power of ruby is that of abstraction and cleanliness.

All else being equal, if I have call to write OO code, I'll always choose
Ruby over Perl. All else is not always equal, and sometimes OOP is not
the most effective approach to solving a problem, however.
 
P

Pit Capitain

2007/7/10 said:
I've also recently adopted the trick of using _ as an unused
parameter name. I believe it was Ara that first suggested this and I
think it's a great idea:

hash.sort_by { |key, _| =85 }=85

Maybe it was Ara indeed, but the first reference I found on ruby-talk
was a post from Nikolai Weibull almost two years ago. See
ruby-talk:149793.

Regards,
Pit
 
R

Robert Dober

All else being equal, if I have call to write OO code, I'll always choose
Ruby over Perl. All else is not always equal, and sometimes OOP is not
the most effective approach to solving a problem, however.

Hmm that's for sure, but is not it's notational beauty which sometimes
makes us use it without any good -other- reason. That is a thought I
am incubating for a long time but your sentence and my recent
experience with Lua pushes me to say that.
I love the concept of Lua, I cannot stand the code I have to write :(.

R.
 
D

dblack

---2049402039-1406859260-1184153004=:13492
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1406859260-1184153004=:13492"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1406859260-1184153004=:13492
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Maybe it was Ara indeed, but the first reference I found on ruby-talk
was a post from Nikolai Weibull almost two years ago. See
ruby-talk:149793.

Isn't that the same as:

hash.sort_by {|key,| ... }

(assuming one isn't going to make use of the value in _)?


David

--=20
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1406859260-1184153004=:13492--
---2049402039-1406859260-1184153004=:13492--
 
R

Robert Dober

Hi --



Isn't that the same as:

hash.sort_by {|key,| ... }

(assuming one isn't going to make use of the value in _)?

yup was brought up by Robert (Klemme) recently, and if memory serves
{ | , key | ... }
does not work :(.Robert
--=20
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
 
J

James Edward Gray II

Maybe it was Ara indeed, but the first reference I found on ruby-talk
was a post from Nikolai Weibull almost two years ago. See
ruby-talk:149793.

Oops, my mistake.

James Edward Gray II=
 
D

diego scataglini

Nice, didn't know about it. Erlang has a similar thing for throwaway =20
values.

Thanks James

Diego Scataglini

On Jul 10, 2007, at 9:23 AM, James Edward Gray II =
<[email protected]=20
wrote:
On Jul 10, 2007, at 08:27 , Christoffer Sawicki wrote:
Hello.

count.keys.sort_by{|key| count[key]}

IMHO, the most elegant way to do this is:

hash.sort_by { |k, v| v }.map { |k, v| k }

...where k =3D key and v =3D value.

Cheers,

Ack, pet peeve.

hash.sort_by { | key, value | value } .map { | key, value | key }

If you have to specify "where k =3D key and v =3D value" then these
should have been used in your code.

Always favor readability at the expense of verbosity, both your
future self and whoever else maintains your code will thank you.

I find myself always using |k,v|, when used on something hashlike I
don't think readability suffers.

I've also recently adopted the trick of using _ as an unused =20
parameter name. I believe it was Ara that first suggested this and =20=
 
A

alex_land

Well i for one am feeling pretty good that i learned Ruby here and not
from a textbook.

Anyway, nice work. Editors are probably wishing they would have gone
with "Cute Code" or "Decent-Looking Code."


--alex
 
R

Robert Dober

Well i for one am feeling pretty good that i learned Ruby here and not
from a textbook.
That is something very nice to say :)
Anyway, nice work. Editors are probably wishing they would have gone
with "Cute Code" or "Decent-Looking Code."

It is indeed a very ambitious title, the Ruby chapter is very
disappointing but the author pointed out it was for beginners (sic).
For me that makes no sense :(

Oh BTW please do not top post, a huge majority of us prefer bottom posting :)
Robert
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top