Where can one find examples of masterful Ruby code?

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

···

On 6/3/06, James Britt <james_b@neurogami.com> 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).

--
.w( the_mindstorm )p.

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

Out of the many suggestions raised in this thread, it seems that
following the Ruby Quiz 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 Quiz 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 Gray II

···

On Jun 3, 2006, at 3:48 PM, Alder Green wrote:

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

···

On 6/3/06, Alder Green <alder.green@gmail.com> 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.

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.

···

On Jun 3, 2006, at 4:48 PM, Alder Green 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))

Alexandru Popescu 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.

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.

···

On 6/3/06, James Britt <james_b@neurogami.com> wrote:

--
James Britt

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

Alder Green 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

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

   It also tells you if i is a multiple of 2 :slight_smile:

···

On 6/4/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

On Jun 3, 2006, at 4:48 PM, Alder Green wrote:

--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

Logan Capaldo 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

···

On Jun 3, 2006, at 4:48 PM, Alder Green wrote:

--
Posted via http://www.ruby-forum.com/\.

Alexandru Popescu 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.

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

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

···

On 6/4/06, James Britt <james_b@neurogami.com> wrote:

> On 6/3/06, James Britt <james_b@neurogami.com> wrote:

--
.w( the_mindstorm )p.

Probably, we have different perception/interpretation of 'mastery'.

--
James Britt

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

2^negative_infinity

add a special case if you like.

-- Elliot Temple

···

On Jun 3, 2006, at 6:17 PM, Ray Baxter wrote:

Alder Green 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?

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

···

On Jun 3, 2006, at 6:39 PM, Christer Nilsson wrote:

Logan Capaldo wrote:

On Jun 3, 2006, at 4:48 PM, Alder Green 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

--
Posted via http://www.ruby-forum.com/\.

Alexandru Popescu wrote:

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.

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

···

On 6/4/06, James Britt <james_b@neurogami.com> wrote:

--
James Britt

"The greatest obstacle to discovery is not ignorance, but the illusion of knowledge."
  - D. Boorstin

> Alder Green 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

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

robert

···

2006/6/4, Elliot Temple <curi@curi.us>:

On Jun 3, 2006, at 6:17 PM, Ray Baxter wrote:

--
Have a look: Robert K. | Flickr

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 Temple

···

On Jun 4, 2006, at 2:47 AM, Robert Klemme wrote:

2006/6/4, Elliot Temple <curi@curi.us>:

On Jun 3, 2006, at 6:17 PM, Ray Baxter wrote:

> Alder Green wrote:
>
> 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