Posted By

AzizLight on 09/04/09


Tagged

textmate decimal ruby terminal binary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Bashelor


Decimal to Binary Terminal Script


 / Published in: Ruby
 

  1. def decimal_to_binar(x)
  2. z = []
  3. while x >= 1
  4. z << x % 2
  5. x /= 2
  6. end
  7. z.reverse.join(" ")
  8. end
  9.  
  10. number = gets.chomp
  11. puts "decimal : #{number}nbinary : #{decimal_to_binar(number.to_i)}"

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: shai on July 22, 2010

why not just do: decimalnum.tos(2) 5.to_s(2) # => "101"

You need to login to post a comment.