Convert decimal to binary and find the maximum number of consecutive 1's in the binary
You solution looks good for a procedural one -- something similar would be the correct way to do this in, say, C.

That said, and assuming this isn't considered "cheating" for the problem at hand, if you're using ruby, you might as well take advantage of ruby's facilities.  Assuming the input number is n, all you need to do is:

n.to_s(2).scan(/1+/).max_by(&amp;:size).size


or, even more tersely, taking advantage of how strings are compared (ie, "11" &gt; "1" and "111" &gt; "11", etc):

n.to_s(2).scan(/1+/).max.size


to_s(2) converts to the binary string, scan finds all runs of 1s, max_by finds the longest of those, and the final size tells you how long that is, as a decimal.