Ruby 将十进制转换为二进制、八进制和十六进制?

Ruby 将十进制转换为二进制、八进制和十六进制?,ruby,Ruby,我试图用Ruby将一个始终是数字的变量转换为二进制、八进制和十六进制 目前我掌握的代码是: def convert(number) puts "#{number} in decimal is" puts "#{number.to_s(2)} in binary" puts "#{number.to_s(8)} in octal" puts "#{number.to_s(16)} in hexadecimal" end 到目前为止,结果是: 2 in decimal is 10

我试图用Ruby将一个始终是数字的变量转换为二进制、八进制和十六进制

目前我掌握的代码是:

def convert(number)
  puts "#{number} in decimal is"
  puts "#{number.to_s(2)} in binary"
  puts "#{number.to_s(8)} in octal"
  puts "#{number.to_s(16)} in hexadecimal"
end
到目前为止,结果是:

2 in decimal is
10 in binary
2 in octal
2 in hexadecimal

前两行运行正常,但之后它将忽略conversion命令,而只是将变量放入。有人知道我错过了什么吗?

你错过了
2
是<代码>2在基数8、16或任何大于2的基数中。尝试
convert(42)
,以获得乐趣。

由于原始问题中的一个小问题而进行向下投票。