# FizzBuzz... But is it the Ruby Way?

Page 1 of 1

## 8 Replies - 2395 Views - Last Post: 18 October 2011 - 07:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251217&amp;s=354c02e411c0ae53b3d3b4acc8ac1ae9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

# FizzBuzz... But is it the Ruby Way?

Posted 13 October 2011 - 05:48 PM

I have coded up two FizzBuzz implementations. They both work but I'm hoping to learn to write ideomatic Ruby. Is one of these better than the other, or is there a better way to solve the problem?

Thanks in advance!

```puts (1..100)
.map{|x| x%15 == 0 ? "FizzBuzz" : x}
.map{|x| x% 3 == 0 ? "Fizz" : x}
.map{|x| x% 5 == 0 ? "Buzz" : x}
.map{|x| x.to_s.ljust(9)}
.join(" ")
```
```puts (1..100).map { |x|
x = "FizzBuzz" if x%15 == 0
x = "Fizz" if x% 3 == 0
x = "Buzz" if x% 5 == 0
x.to_s.ljust(9)
}.join(" ")
```

Is This A Good Question/Topic? 1

## Replies To: FizzBuzz... But is it the Ruby Way?

### #2 brep

• D.I.C Head

Reputation: 23
• Posts: 173
• Joined: 26-August 11

## Re: FizzBuzz... But is it the Ruby Way?

Posted 14 October 2011 - 11:49 AM

In my opinion, I think the second is a little cleaner, since you don't have to constantly repeat the .map constantly, as in the first one. It also looks more organized.

This is only my opinion, so I may be wrong, but it still does look cleaner than the first.

This post has been edited by preb: 14 October 2011 - 01:44 PM

Was This Post Helpful? 1

### #3 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

## Re: FizzBuzz... But is it the Ruby Way?

Posted 16 October 2011 - 04:38 PM

Thanks. Just out of interest, how would you have done it? Would it be similar to my second one or totally different?
Was This Post Helpful? 0

### #4 brep

• D.I.C Head

Reputation: 23
• Posts: 173
• Joined: 26-August 11

## Re: FizzBuzz... But is it the Ruby Way?

Posted 16 October 2011 - 05:24 PM

Since I am beginning ruby myself, I would have used loops (such as an upto loop or do loop) instead of the puts loop (if that's what it's called) you did. I would have included if statements (as you did), and I would not have used .map, mainly because I have not learned how to use it yet.

So overall, it would have been similar to what you did

preb
Was This Post Helpful? 1

### #5 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

## Re: FizzBuzz... But is it the Ruby Way?

Posted 16 October 2011 - 06:15 PM

OK, thanks for the input.
Was This Post Helpful? 0

### #6 Skaggles

• THE PEN IS MIGHTIER

Reputation: 255
• Posts: 641
• Joined: 01-March 09

## Re: FizzBuzz... But is it the Ruby Way?

Posted 17 October 2011 - 06:15 PM

To expand on what said, using loops would look very similar to what you've done. Perhaps, something like this:

```(1..100).each do |i|
txt = ""
txt += "Fizz" if i % 3 == 0
txt += "Buzz" if i % 5 == 0
puts (txt.empty?) ? i : txt
end

```

or with if/else

```(1..100).each do |i|
if i % 15 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
end

```

There are several ways that this can be coded in Ruby and there will always be people debating which is the "correct" way in which to do so. I suggest just experimenting with the different methods and learning more about Ruby in the process.
Was This Post Helpful? 4

### #7 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

## Re: FizzBuzz... But is it the Ruby Way?

Posted 17 October 2011 - 06:24 PM

Quote

```(1..100).each do |i|
txt = ""
txt += "Fizz" if i % 3 == 0
txt += "Buzz" if i % 5 == 0
puts (txt.empty?) ? i : txt
end
```

Oh, I like this one! Thanks for the advice too!
Was This Post Helpful? 0

### #8 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• Joined: 21-June 11

## Re: FizzBuzz... But is it the Ruby Way?

Posted 18 October 2011 - 05:52 AM

One problem I see with the first solution, which wasn't mentioned yet, is that you're using % on strings in a non-obvious way. It took me a moment to realize that the reason your code actually worked is that "fizzbuzz" % n just returns "fizzbuzz" (and "fizzbuzz" is indeed not equal to 0) without error because String#% ignores any superfluous arguments.

Basically the code works more or less by accident because there happens to be a % method for strings and it happens to function in a way compatible to your usage in this case. However relying on that seems bad practice to me. In general I'd advice against creating heterogeneous arrays like this.
Was This Post Helpful? 2

### #9 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

## Re: FizzBuzz... But is it the Ruby Way?

Posted 18 October 2011 - 07:06 AM

Oh, good catch! I didn't pick up on that at all.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}