8 Replies - 1727 Views - Last Post: 18 October 2011 - 07:06 AM Rate Topic: -----

#1 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 166
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 166
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • 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  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • 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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,177
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • 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