My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
I've been wanting to do a Ruby Quiz now for quite some time, but I've always had some excuse about being too busy. But this week's Ruby Quiz is FizzBuzz, so I had to take the two minutes and write up a solution.

Some of the discussion on Ruby Talk has been around getting the smallest solution possible (even though the quiz directs us to do as we might for a job interview). There are plenty of claims ranging from 56 to 72 bytes. I couldn't even get under 100, so I'm quite interested to see the different solutions when they are published - not because I think code so terse as to be unintelligible is something we should strive for, but because surely I'll learn new tricks (and some of which I'd expect are understandable!).

Anyway, here's my Ruby FizzBuzz (how exciting):

class Integer
  def fizzbuzz
    fizzable = self % 3 == 0
    buzzable = self % 5 == 0
    print "Fizz" if fizzable
    print "Buzz" if buzzable
    print self if !(fizzable || buzzable)

(1..100).each {|i|  i.fizzbuzz}

The one part where I did learn something was that self gets me the member variable holding the integer here. I didn't know that before, and I started out looking for "how to find the name of the member variable in Ruby Integer class" (or some such search query). When I didn't find anything, I thought I'd try the obvious - self. Of course, like many things in Ruby, it followed the principle of least surprise, and my guess worked.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!

Leave a comment

You know, I was thinking a better implementation would be for Integer#fizzbuzz to return a string, and instead puts i.fizzbuzz in the loop. That would certainly shave off many characters, and more importantly make the program have a better design (in my opinion).

Posted by Sam on Jun 02, 2007 at 06:46 PM UTC - 5 hrs

I got it down to 96 characters with this beast, so I'm guessing to shave another 20 off of that (and 40 for the best claim), I'd need to drastically change my approach:

def z i;r="";r+="Fizz" if i%3==0;r+="Buzz" if i%5==0;r=i if r=="";r;end;1.upto(100){|i|puts z i}

Posted by Sam on Jun 02, 2007 at 07:06 PM UTC - 5 hrs

So, the main idea that removed the bytes was the ternary operator. I abhor trying to figure code like that out, so I didn't even think to use it. =) Lesson learned, and next time I go golfing (see I'll be sure to have it in my toolkit.

Posted by Sammy Larbi on Jun 04, 2007 at 09:30 AM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner