Dylan Poirier
Dylan Poirier

Reputation: 5

Convert decimal to binary in ruby

I am trying to convert a decimal to binary, so I came up with this:

print "Enter decimal here: "
decimal = gets.chomp
puts decimal.to_s(2)
#>> wrong number of arguments (given 1, expected 0)
#>> (repl):3:in 'to_s'

Can someone tell me what I am doing wrong?

Upvotes: 0

Views: 367

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

decimal comes from stdin as a string. And String#to_s does not accept arguments. You should do instead:

puts decimal.to_i.to_s(2)

Upvotes: 2

Related Questions