Beautiful Code : Pity he didn't ask here

On 7/10/07, Tim B. [email protected] wrote:

Yep, mostly because for some reason I didn’t know about sort_by. I
wonder how I missed that? O

Perhaps because sort_by is defined in Enumerable, not in the Array
class. I wish that the Ruby documentation inlined methods like these
from Mixin classes (similar to how Javadoc displays a parent class’
methods). Is this possible when generating using rdoc?


Caleb

Doubt is not a pleasant condition, but certainty is absurd - Voltaire

On Jul 10, 2007, at 8:47 AM, Tim B. wrote:

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

Hi –

On Wed, 11 Jul 2007, ara.t.howard wrote:

On Jul 10, 2007, at 8:47 AM, Tim B. wrote:

Gosh, I think I totally disagree on readability grounds.

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

I fell in love :slight_smile:

David

On Jul 10, 2007, at 8:28 AM, James Edward G. II wrote:

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 … :wink:

-a

On Wed, Jul 11, 2007 at 10:53:42AM +0900, ara.t.howard wrote:

%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

On Jul 10, 2007, at 8:53 PM, ara.t.howard wrote:

On Jul 10, 2007, at 8:47 AM, Tim B. wrote:

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

You just insulted all of my best friends in one go. I think I better
get my butt back to Perl… :slight_smile:

James Edward G. II

On Wed, Jul 11, 2007 at 10:53:42AM +0900, ara.t.howard wrote:

On Jul 10, 2007, at 8:47 AM, Tim B. wrote:

Gosh, I think I totally disagree on readability grounds.

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).

On Jul 10, 2007, at 8:08 PM, Marcel Molina Jr. wrote:

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

On Jul 10, 2007, at 8:43 PM, Chad P. wrote:

repertoire. I’m not sold on the idea that there’s One True

  • wtf is $1 ?

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. :wink:

-a

On 7/11/07, ara.t.howard [email protected] wrote:

On Jul 10, 2007, at 8:28 AM, James Edward G. II wrote:

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 … :wink:

Admit it, you took it from Prolog or Lua or Python or … :slight_smile:

Robert

On Jul 10, 2007, at 8:15 PM, James Edward G. II wrote:

You just insulted all of my best friends in one go.

many of mine too. still, there are only a few of my friends i’d
exclaim to be beautiful! :wink:

-a

On Jul 10, 2007, at 9:08 PM, Marcel Molina Jr. wrote:

captures.

marcel

Marcel Molina Jr. [email protected]

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!

On Wed, Jul 11, 2007 at 01:07:55PM +0900, ara.t.howard wrote:

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.

2007/7/10, James Edward G. II [email protected]:

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, _| … }…

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

Regards,
Pit

On 7/11/07, Chad P. [email protected] wrote:

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.

Hi –

On Wed, 11 Jul 2007, Pit C. wrote:

2007/7/10, James Edward G. II [email protected]:

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, _| … }…

Maybe it was Ara indeed, but the first reference I found on ruby-talk
was a post from Nikolai W. 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

On 7/11/07, [email protected] [email protected] wrote:

Maybe it was Ara indeed, but the first reference I found on ruby-talk
was a post from Nikolai W. 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 _)?

yup was brought up by Robert (Klemme) recently, and if memory serves
{ | , key | … }
does not work :(.

Robert

On Jul 11, 2007, at 6:11 AM, Pit C. wrote:

2007/7/10, James Edward G. II [email protected]:

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, _| … }…

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

Oops, my mistake.

James Edward G. II

On Jul 10, 10:28 am, James Edward G. II [email protected]
wrote:

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

James Edward G. II

Just as an aside, I believe Erlang adopts this style as a part of this
language, where it will warn if you have unused variables but allows
you to annotate them with _ (ex. _var) to denote that they will not be
used, or just replace the variable name with _ thus eliminating the
warning.

(I don’t know Erlang yet, just remembered reading this in
http://pragdave.pragprog.com/pragdave/2007/04/a_first_erlang_.html)

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

Thanks James

Diego Scataglini

On Jul 10, 2007, at 9:23 AM, James Edward G. II
<[email protected]