convert a number from base 10 to base 2 in ruby

To convert a number from base 10 to base 2 in ruby, you can use the to_s method with a parameter of 2 to convert the number to a binary string:

main.rb
number = 42
binary_string = number.to_s(2)
puts binary_string
# Output: 101010
79 chars
5 lines

In this example, the number 42 is converted to the binary string "101010" using the to_s method with a parameter of 2. The resulting string can then be manipulated or displayed as needed.

gistlibby LogSnag