convert a number from base 2 to base 10 in ruby

To convert a number from base 2 to base 10 in Ruby, you can use the to_i method with the base 2 as an argument, and then pass the resulting number as an argument to the to_s method with base 10 as an argument. Here's an example:

main.rb
binary_num = "1010"
decimal_num = binary_num.to_i(2).to_s(10)
puts decimal_num
79 chars
4 lines

Output:

main.rb
10
3 chars
2 lines

gistlibby LogSnag