Forum: Ruby Where can one find examples of masterful Ruby code?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alder G. (Guest)
on 2006-06-02 00:32
(Received via mailing list)
Hi

To start shortening the gap between myself - an average Ruby
programmer - and the top Ruby hackers out there, I'm looking for some
good Ruby code to read. They say you should learn from the masters, so
please tell me: who are those masters, and which of their works are
masterpieces?
Greg D. (Guest)
on 2006-06-02 00:38
(Received via mailing list)
On 6/1/06, Alder G. <removed_email_address@domain.invalid> wrote:
> To start shortening the gap between myself - an average Ruby
> programmer - and the top Ruby hackers out there, I'm looking for some
> good Ruby code to read. They say you should learn from the masters, so
> please tell me: who are those masters, and which of their works are
> masterpieces?


Rubyquiz.com has some very high quality code examples.  The Best Of
Ruby Q. book is really good too.
Tyler P. (Guest)
on 2006-06-02 00:45
(Received via mailing list)
Hopefully you'll get more helpful answers to this question from someone
a
little more knowledgable, but a few examples of fantastic code (although
often complicated) are the ruby library, rails, and ruby quiz.  Ruby
Quiz
also has the added benefit of a discussion and breakdown of each week's
program by James Edward G. II.  Check it out here:
http://www.rubyquiz.com/

--Tyler
Son SonOfLilit (Guest)
on 2006-06-02 01:07
(Received via mailing list)
Why the lucky stiff. Camping. Markaby. Cartoon foxes. I should read his
code
one day too.

Rails, too, amazing reflecting code. That's ActiveRecord I'm talking
about.

Whytheluckystiff's blog "redhanded" has many "masterful code"
spotlights, by
him and others. Check it out.

Time, if I only had a bit more of it...
Alder G. (Guest)
on 2006-06-02 01:19
(Received via mailing list)
On 6/1/06, Tyler P. <removed_email_address@domain.invalid> wrote:
> Hopefully you'll get more helpful answers to this question from someone a
> little more knowledgable, but a few examples of fantastic code (although
> often complicated) are the ruby library, rails, and ruby quiz.

Being a Rails programmer, I tried reading some of Rails' code and it
looks like you have to commit to reading a large portion of it to gain
any understanding. At least the core stuff that I sampled
(ActiveRecord, ActionPack) looks very tightly knit together. A guide
about approaching the Rails codebase might be very helpful, and
beneficial considering the importance of codebase accessibility in an
OSS project. Maybe I should raise this issue in the Rails ML.
James B. (Guest)
on 2006-06-02 01:38
(Received via mailing list)
Alder G. wrote:
> (ActiveRecord, ActionPack) looks very tightly knit together. A guide
> about approaching the Rails codebase might be very helpful, and
> beneficial considering the importance of codebase accessibility in an
> OSS project. Maybe I should raise this issue in the Rails ML.
>


Semi-rhetorical questions:  If you are not already well-versed in Ruby,
how do you know you are reading masterful code?

If you are reading masterful code, can one appreciate what makes it
masterful without a firm knowledge of Ruby?

Is the definition of "masterful" conditional?  If so, on what?

James

--
James B.

"A principle or axiom is of no value without the rules for applying it."
   - Len Bullard
A. S. Bradbury (Guest)
on 2006-06-02 02:41
(Received via mailing list)
On Thursday 01 June 2006 21:31, Alder G. wrote:
> Hi
>
> To start shortening the gap between myself - an average Ruby
> programmer - and the top Ruby hackers out there, I'm looking for some
> good Ruby code to read. They say you should learn from the masters, so
> please tell me: who are those masters, and which of their works are
> masterpieces?

On top of what others have suggested, I think eigenclass.org may provide
a lot
of what you are looking for. Also, the code browser on RAA (Gonzui):
http://raa.ruby-lang.org/gonzui/markup provides a very handy interface
for
browsing through random code. If you see a project you've heard about or
used, or one that just looks interesting why not look through?
Certainly, you
may not always be looking at great Ruby code, but I think you can learn
a lot
this way.

Hope this helps,
Alex
ReggW (Guest)
on 2006-06-02 12:46
James B. wrote:
> Alder G. wrote:
>
> Semi-rhetorical questions:  If you are not already well-versed in Ruby,
> how do you know you are reading masterful code?
>
> If you are reading masterful code, can one appreciate what makes it
> masterful without a firm knowledge of Ruby?
>
> Is the definition of "masterful" conditional?  If so, on what?
>
Shut-up and just point us to the "masterful" code. :)
Simon S. (Guest)
on 2006-06-02 14:37
(Received via mailing list)
On 6/2/06, ReggW <removed_email_address@domain.invalid> wrote:
[snip]
> Shut-up and just point us to the "masterful" code. :)


Have a look at the ruby code distributed with the ruby.

If you are looking for a large scale masterful project consisting of
tons of files, then you will have to look elsewhere.
Robert K. (Guest)
on 2006-06-02 15:14
(Received via mailing list)
2006/6/2, ReggW <removed_email_address@domain.invalid>:
> James B. wrote:
> > Semi-rhetorical questions:  If you are not already well-versed in Ruby,
> > how do you know you are reading masterful code?
> >
> > If you are reading masterful code, can one appreciate what makes it
> > masterful without a firm knowledge of Ruby?
> >
> > Is the definition of "masterful" conditional?  If so, on what?
> >
> Shut-up and just point us to the "masterful" code. :)

class Master
  def ful() "code" end
end

Oh, sorry, it has to fit on one line of course:

class Master; def ful() "code" end end
;-)

SCNR

robert
Ross B. (Guest)
on 2006-06-02 15:39
(Received via mailing list)
On Fri, 2006-06-02 at 05:31 +0900, Alder G. wrote:
> Hi
>
> To start shortening the gap between myself - an average Ruby
> programmer - and the top Ruby hackers out there, I'm looking for some
> good Ruby code to read. They say you should learn from the masters, so
> please tell me: who are those masters, and which of their works are
> masterpieces?
>

As well as RubyQuiz and the other suggestions, I'd recommend looking
into Facets (http://facets.rubyforge.org). A lot of the people I
consider to be Ruby masters have written code either for that project,
or that's found it's way in there. An added bonus is: it's lots of small
extensions (facets :)) so you don't have to understand the whole to
understand the individual bits.

You might have to install it though, I couldn't find any SCM links on
the project page...
Ross B. (Guest)
on 2006-06-02 15:54
(Received via mailing list)
On Fri, 2006-06-02 at 20:13 +0900, Robert K. wrote:
> > Shut-up and just point us to the "masterful" code. :)
>
> class Master
>   def ful() "code" end
> end
>
> Oh, sorry, it has to fit on one line of course:
>
> class Master; def ful() "code" end end

Or, not quite equivalent:

def (Master=[]).ful() "code" end

;)
John G. (Guest)
on 2006-06-02 21:54
(Received via mailing list)
On 6/1/06, James B. <removed_email_address@domain.invalid> wrote:
> > any understanding. At least the core stuff that I sampled
> If you are reading masterful code, can one appreciate what makes it
> masterful without a firm knowledge of Ruby?
>
> Is the definition of "masterful" conditional?  If so, on what?

I think you might have your tongue in your cheek asking that last one,
but I'll answer that it's only masterful if it's well documented. That
is: if other devs can't jump in and easily behold your mastery, than
what sort of a master are you?

What good is pulling of a daring coup in your code if no one sees why
(or if it takes way too long to figure out how) you did it?

---John
Chris P. (Guest)
on 2006-06-02 22:06
(Received via mailing list)
> class Master; def ful() "code" end end

It's not what it does right, it's what it *doesn't* do wrong.

(right?)
carlos tirado (Guest)
on 2006-06-02 22:49
(Received via mailing list)
On 6/1/06, Alder G. <removed_email_address@domain.invalid> wrote:
> I'm looking for some good Ruby code to read.

I don't know about "masterful" but I've found plenty to learn from
these resources (They all have RSS feeds):
http://blog.grayproductions.net/
http://redhanded.hobix.com/
http://eigenclass.org/
http://www.oreillynet.com/ruby/

HIH

C
Christian N. (Guest)
on 2006-06-03 19:55
(Received via mailing list)
James B. <removed_email_address@domain.invalid> writes:

> Semi-rhetorical questions:  If you are not already well-versed in
> Ruby, how do you know you are reading masterful code?
>
> If you are reading masterful code, can one appreciate what makes it
> masterful without a firm knowledge of Ruby?

I'd say that I'm pretty well-versed in Ruby.  I have not yet seen much
masterful code.  I'm not sure I could point out a medium-sized piece
of Ruby that's *truly* and *completely* masterful.  Sure, the "Quality
Without A Name" turns up here and there, but I just don't find
something that is completely soaked with it.  Maybe I just have too
high expectations.  Prove me wrong, please.

> Is the definition of "masterful" conditional?  If so, on what?

Does masterful code get less and less the more you advance?
Does masterful code only exist in languages you don't know that well?
(I somehow feel like there is plenty of masterful Haskell code out
there...)
Alexandru P. (Guest)
on 2006-06-03 20:29
(Received via mailing list)
On 6/3/06, Christian N. <removed_email_address@domain.invalid> wrote:
> of Ruby that's *truly* and *completely* masterful.  Sure, the "Quality
>
I'd say that this depends very much on your definition of "masterful"
code. In case it means, code that you're not able yet to write, than
indeed, in time you will find less and less (hopefully :-) ). IMO
'masterful' code just means, nice, clean and following the best
practices of the language/domain addressed by that code. After about
10 years of Java, I can find 'masterful' code in a lot of places.

./alex
--
.w( the_mindstorm )p.
James B. (Guest)
on 2006-06-03 22:15
(Received via mailing list)
Christian N. wrote:
> I'd say that I'm pretty well-versed in Ruby.  I have not yet seen much
> masterful code.  I'm not sure I could point out a medium-sized piece
> of Ruby that's *truly* and *completely* masterful.  Sure, the "Quality
> Without A Name" turns up here and there, but I just don't find
> something that is completely soaked with it.  Maybe I just have too
> high expectations.  Prove me wrong, please.

The code I've seen that struck me as masterful (insert all the usual
caveats about judgment, qualifications, etc.) tended to be short, crisp,
and fairly easy to understand.  Sometimes there was an "Ah ah!" moment
in grokking the code.

Most code, though, seems focused on getting a job done, which makes
perfect sense.  So I don't expect to find files filled with line after
line of masterful code; I think it more likely that the more skilled
Rubyists will write a collection of relatively short, clear methods that
make it easier to write the rest of the code needed to accomplish a
particular task.  So most of the code appears, at first, quite mundane,
until you realize how much *more* mundane code would be required were it
not for these enabling methods.

There may be analogies to furniture making, where a piece may not strike
you as especially masterful until you poke around and see how the hidden
joints were constructed, and how they enable a design that in other,
less-skilled hands, would never be stable.


>
>
>>Is the definition of "masterful" conditional?  If so, on what?
>
>
> Does masterful code get less and less the more you advance?
> Does masterful code only exist in languages you don't know that well?
> (I somehow feel like there is plenty of masterful Haskell code out
> there...)

It may be like music, where the more one knows and the more one has
heard, the harder it is to get excited about new music (though when you
do find something good, it can be *really* exciting).



--
James B.

"I often work by avoidance."
- Brian Eno
Christian N. (Guest)
on 2006-06-03 23:10
(Received via mailing list)
"Alexandru P." <removed_email_address@domain.invalid> writes:

> I'd say that this depends very much on your definition of "masterful"
> code. In case it means, code that you're not able yet to write, than
> indeed, in time you will find less and less (hopefully :-) ). IMO
> 'masterful' code just means, nice, clean and following the best
> practices of the language/domain addressed by that code. After about
> 10 years of Java, I can find 'masterful' code in a lot of places.

Oh.  I just call that "good" code.  It can be found rather easily,
especially in the Ruby community.  :-)
Alder G. (Guest)
on 2006-06-04 00:50
(Received via mailing list)
On 6/3/06, James B. <removed_email_address@domain.invalid> wrote:
> >
> in grokking the code.
> There may be analogies to furniture making, where a piece may not strike
> > Does masterful code get less and less the more you advance?
> --
> James B.
>
> "I often work by avoidance."
> - Brian Eno
>
>

Obviously, the adjective "masterful" is not well defined;  I should
clarify what I meant.

"Masterful code" is a solution for a problem you wouldn't be able to
solve by yourself, although it only requires knowledge you possess.
Such quality is easily idetifieable by the "aha!" moment you
experience upon encountering it. That "aha!" quality would be
intensified by first attempting to solve the problem yourself. Success
in this stage would also be accompanied by an "aha!" moment.

For example: check if integer i is a power of 2, with an expression no
more than 13 characters long.

Solution: (i & (i - 1))

Note that this solution is not only very concise, but also (on any
reasonable implementation) very fast. Masterful code has a tendency to
be "correct" in many ways.

In a sense I agree with your music analogy, though I believe masterful
code should be more prevalent than inspiring music. Chess would form a
closer analogy, and if no combination surprises you then your name
must be Bobby Fischer (but even he would manage to find opponents to
surprise him by searching the top circles).

You may argue that "masterful code" as such is independent of
langauge. You would be correct to conclude that. The above example
works, with little or no change, in many languages. I was asking for
masterful code in Ruby not because I'm using it, but because of the
reason which lead me to use it:

Ruby seems to encourage masterful code. It allows ideas to be
expressed directly. The other langauge I'd consider to encourage
masterful code is Scheme. But Ruby manages to offer many more tools
than Scheme. An elegant Ruby solution may rely on classes or regular
expressions. In fact Ruby seems to aspire to equip the programmer with
as many tools as possible. Which is a Good Thing. It thus may be the
best language (that I know) for the masterful progrmmer.

Out of the many suggestions raised in this thread, it seems that
following the Ruby Q. would be most appropriate. By such
participation much improvement might be gained. In such competitions,
the winning solutions are frequently required to be "masterful code"
according to the above definition.

Also it seems one should look into Ruby F.s, not merely because it
appears  to be a gathering point for Ruby masters but also because it
looks damn useful.

And I should read the source of projects widely considered masterful,
written by programmers widely considered to be masters. Rails seems a
fitting candidate in that respect, being recommended on both accounts.
James G. (Guest)
on 2006-06-04 00:59
(Received via mailing list)
On Jun 3, 2006, at 3:48 PM, Alder G. wrote:

> For example: check if integer i is a power of 2, with an expression no
> more than 13 characters long.
>
> Solution: (i & (i - 1))

Yuck.  Thanks for reminding me that "masterful code" is definitely in
the eye of the beholder.  ;)

> Out of the many suggestions raised in this thread, it seems that
> following the Ruby Q. would be most appropriate. By such
> participation much improvement might be gained. In such competitions,
> the winning solutions are frequently required to be "masterful code"
> according to the above definition.

Ruby Q. isn't about "winning."  It's just a collection of fun
exercises to keep us sharp.  Everyone who plays or follows along is a
winner, in my opinion.

James Edward G. II
Phil T. (Guest)
on 2006-06-04 01:36
(Received via mailing list)
On 6/3/06, Alder G. <removed_email_address@domain.invalid> wrote:
 >
> For example: check if integer i is a power of 2, with an expression no
> more than 13 characters long.
>
> Solution: (i & (i - 1))
>
> Note that this solution is not only very concise, but also (on any
> reasonable implementation) very fast. Masterful code has a tendency to
> be "correct" in many ways.
>

Thanks for this very useful snippet.  It was worth reading the thread
for.

Probably should be methodized as:
  def powerof2?( i )
    (i & (i - 1))==0
  end

Of course it's not immediately obvious from reading the code how it
works till you try an example:

   1000  #8 in binary
   0111  #7 in binary
 &----------
   0000

...so maybe readability isn't always part of the criteria for
'masterful' code.  I do think that the way you've done it is the
fastest way to figure out if an integer is a power of 2 and after you
work through an example it does seem quite elegant.

Phil
Logan C. (Guest)
on 2006-06-04 02:01
(Received via mailing list)
On Jun 3, 2006, at 4:48 PM, Alder G. wrote:

> For example: check if integer i is a power of 2, with an expression no
> more than 13 characters long.
>
> Solution: (i & (i - 1))

I'm confused.


irb(main):001:0> i = 9
=> 9
irb(main):002:0> (i & (i - 1))
=> 8

So 9 is a power of 2?

As far as 13 characters goes

(i % 2).zero?

is _exactly_ 13 characters and it gives a true or false answer
instead of zero of something else.
Vlad GALU (Guest)
on 2006-06-04 02:07
(Received via mailing list)
On 6/4/06, Logan C. <removed_email_address@domain.invalid> wrote:
>
>
> is _exactly_ 13 characters and it gives a true or false answer
> instead of zero of something else.

   It also tells you if i is a multiple of 2 :)
Alexandru P. (Guest)
on 2006-06-04 02:17
(Received via mailing list)
On 6/3/06, James B. <removed_email_address@domain.invalid> wrote:
> It may be like music, where the more one knows and the more one has
> heard, the harder it is to get excited about new music (though when you
> do find something good, it can be *really* exciting).
>

Never heard or thought this way. It would mean that the top musicians
are really bored by the music they play in concerts. I guess that the
more you know the more you are able to identify wonderful spot and
enjoy them.

./alex
Christer N. (Guest)
on 2006-06-04 02:38
Logan C. wrote:
> On Jun 3, 2006, at 4:48 PM, Alder G. wrote:
>
>> For example: check if integer i is a power of 2, with an expression no
>> more than 13 characters long.
>>
>> Solution: (i & (i - 1))
>
> I'm confused.
>
>
> irb(main):001:0> i = 9
> => 9
> irb(main):002:0> (i & (i - 1))
> => 8
>
> So 9 is a power of 2?
>
> As far as 13 characters goes
>
> (i % 2).zero?
>
> is _exactly_ 13 characters and it gives a true or false answer
> instead of zero of something else.

Actually you have to compare with zero to get the expected result as
a previous poster posted:

  def powerof2?( i )
    (i&(i-1))==0
  end

(i&(i-1))==0 counts to twelve characters.

i%2 determines odd or even.

powerof2?(9) => false
powerof2?(16) => true


/christer
James B. (Guest)
on 2006-06-04 03:24
(Received via mailing list)
Alexandru P. wrote:
> enjoy them.
Well, for myself, the music I like to listen to, and the music I like to
write, and the music I like to play, are different sets with only
partial overlap.

For example, I like a lot of droning, repetitive, minimalist music, but
hate playing it.  And I like playing, say, songs in the vein of the Sex
Pistols, but have no interest in *writing* anything that sounds like
that.

In my code/music metaphor I was thinking more of what I find interesting
to listen to; less and less makes a big impression on me (though this
could just be a sign that I'm turning into my parents).

I suspect that with various programming languages, as you internalize
the idioms, you're less struck by how clever something is; you may
think, "That's just how it's done."

There's less novelty for you to misinterpret as mastery.


--
James B.

http://web2.0validator.com    - We're the Dot in Web 2.0
http://refreshingcities.org   - Design, technology, usability
http://yourelevatorpitch.com  - Finding Business Focus
http://www.jamesbritt.com     - Playing with Better Toys
Logan C. (Guest)
on 2006-06-04 04:15
(Received via mailing list)
On Jun 3, 2006, at 6:39 PM, Christer N. wrote:

>>
>> (i % 2).zero?
>
> --
> Posted via http://www.ruby-forum.com/.
>

I was trying to make a point. Unfortunately for my point making, I
misunderstood the function. I was trying to come up with a reasoned
argument for this but I can't, and my example sucked. So I just want
to say "(n & (n - 1)) is icky."
Alexandru P. (Guest)
on 2006-06-04 04:18
(Received via mailing list)
On 6/4/06, James B. <removed_email_address@domain.invalid> wrote:
> > more you know the more you are able to identify wonderful spot and
> In my code/music metaphor I was thinking more of what I find interesting
> to listen to; less and less makes a big impression on me (though this
> could just be a sign that I'm turning into my parents).
>
> I suspect that with various programming languages, as you internalize
> the idioms, you're less struck by how clever something is; you may
> think, "That's just how it's done."
>

So, what you are trying to say is that by the time you may become
blase. If so, than where is the passion? Has all burnt down? Going
back to code/music metaphor, this would translate that most of the
composers would just stop composing because they have enough. And I
really cannot agree with this.

> There's less novelty for you to misinterpret as mastery.
>

It looks like we are having a different perspective/interpretation of
mastery. And probably, we should agree to disagree. For me a
masterpiece will still be a masterpiece, whatever my understanding
level will be.

best regards,

./alex
--
.w( the_mindstorm )p.


Probably, we have different perception/interpretation of 'mastery'.
Ray B. (Guest)
on 2006-06-04 05:19
(Received via mailing list)
Alder G. wrote:

> For example: check if integer i is a power of 2, with an expression no
> more than 13 characters long.
>
> Solution: (i & (i - 1))

0 is a power of 2?

--

Ray
Elliot T. (Guest)
on 2006-06-04 05:50
(Received via mailing list)
On Jun 3, 2006, at 6:17 PM, Ray B. wrote:

> Alder G. wrote:
>
>> For example: check if integer i is a power of 2, with an
>> expression no
>> more than 13 characters long.
>> Solution: (i & (i - 1))
>
> 0 is a power of 2?

2^negative_infinity

add a special case if you like.

-- Elliot T.
http://www.curi.us/blog/
James B. (Guest)
on 2006-06-04 06:11
(Received via mailing list)
Alexandru P. wrote:
> On 6/4/06, James B. <removed_email_address@domain.invalid> wrote:

> So, what you are trying to say is that by the time you may become
> blase.

No; that's too cynical.  I'm saying that naivety distorts appreciation.

I expect that 99% of the people on this list have gone back to look at
code they wrote some time ago, code they were quite proud of at the
time, and cringed.  Or created what they think is some neat hack, rushed
of to show their more experienced co-workers, only to learn that they've
finally discovered a common idiom of the language.  Acquiring a more
critical judgment is not being blase.

> If so, than where is the passion? Has all burnt down?

"I don't care for it" is not the same as "I don't care."

There's a difference between judging something to be crap, and believing
that it must be that way by necessity.

> Going
> back to code/music metaphor, this would translate that most of the
> composers would just stop composing because they have enough. And I
> really cannot agree with this.

Well, that's a misreading of what I wrote.  Composers may be more driven
as they learn more, because the challenge gets harder, while expereinced
listeners have a diminissed tolerance for crap, because over time poor
music doesn't bear up.  So they demand better.

>
>> There's less novelty for you to misinterpret as mastery.
>>
>
> It looks like we are having a different perspective/interpretation of
> mastery. And probably, we should agree to disagree. For me a
> masterpiece will still be a masterpiece, whatever my understanding
> level will be.


But the ability to recognize a masterpiece should change with your level
of understanding, and that's my point.  People looking for masterful
code need some frame of reference both to spot it and to appreciate it.

Asking for examples of masterful code is only half the journey.

Growing up, people told me about masterful composers, and I listened to
their works, but it was only years later, after exposure to a lot of
music, that I could really appreciate what made a work great (or not).


--
James B.

"The greatest obstacle to discovery is not ignorance, but the illusion
of knowledge."
  - D. Boorstin
Robert K. (Guest)
on 2006-06-04 13:50
(Received via mailing list)
2006/6/4, Elliot T. <removed_email_address@domain.invalid>:
> > 0 is a power of 2?
>
> 2^negative_infinity

Wow, this actuall works in Ruby:

irb(main):004:0> -1.0/0
=> -Infinity
irb(main):007:0> 2 ** (-1.0/0)
=> 0.0

> add a special case if you like.

... and violate the 13 character constraint. :-)

robert
Elliot T. (Guest)
on 2006-06-04 20:15
(Received via mailing list)
On Jun 4, 2006, at 2:47 AM, Robert K. wrote:

> Wow, this actuall works in Ruby:
>
> irb(main):004:0> -1.0/0
> => -Infinity
> irb(main):007:0> 2 ** (-1.0/0)
> => 0.0

maybe that's a rounding error?

irb(main):009:0> 2 ** (-2 ** 11)
=> 0.0


irb(main):008:0> 2.0 ** (-2.0 ** 11.0)
=> 0.0

(i'm not sure exactly how conversion to floats works, hence 2 versions)


-- Elliot T.
http://www.curi.us/blog/
This topic is locked and can not be replied to.