Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用数组将给定数字转换为单词_Ruby - Fatal编程技术网

Ruby 使用数组将给定数字转换为单词

Ruby 使用数组将给定数字转换为单词,ruby,Ruby,我正在尝试创建一种方法,用于经典瓶装啤酒的输出 给定一个数字,@beers,我想将其转换为该数字的word版本。我想将数字拆分为一个数组,然后与一个数字列表进行比较: class BeerSong attr_accessor :beers def initialize(beers) if beers > 99 beers = 99 elsif beers < 1 beers = "Zero" end @beers

我正在尝试创建一种方法,用于经典瓶装啤酒的输出

给定一个数字,
@beers
,我想将其转换为该数字的word版本。我想将数字拆分为一个数组,然后与一个数字列表进行比较:

class BeerSong

  attr_accessor :beers

  def initialize(beers)

    if beers > 99
      beers = 99
    elsif beers < 1
      beers = "Zero"
    end
    @beers = beers
  end


  def worded
    num_name = {      
      90 => "Ninety", 80 => "Eighty", 70 => "Seventy",60 => "Sixty",
      50 => "Fifty",40 => "Forty",30 => "Thirty",20 => "Twenty",
      19 => "Nineteen", 18 => "Eighteen",  17 => "Seventeen", 16 => "Sixteen",
      15 => "Fifteen",14 => "Fourteen", 13 =>"Thirteen",  12 => "Twelve",
      11 => "Eleven", 10 => "Ten",  9 => "Nine",8 => "Eight", 7 => "Seven",
      6 => "Six",5 => "Five",4 => "Four",3 => "Three",2 => "Two",1 => "One"
    }


    worded = ""

    beers = @beers

    split_beers = beers.to_s.split

    num_name.each do |number, name|
      split_number = number.to_s.split
      if beers == number
        worded << name
      else
        number > 19 && split_number[0].to_i == split_beers[0].to_i
        worded << name
        worded << "-"
      end
    end

    num_name.each do |number, name|
      if number < 10 && split_beers[1].to_i == number
        worded << name
      end
    end

    worded

  end

  def print_song

    while @beers.to_i > 2

      puts "#{worded} bottles of beer on the wall,"
      puts "#{worded} bottles of beer,"
      puts "Take one down, pass it around,"
      @beers -= 1
      puts "#{worded} bottles of beer on the wall.\n\n"
    end

    if @beers.to_i == 2
      puts "#{worded} bottles of beer on the wall,"
      puts "#{worded} bottles of beer,"
      puts "Take one down, pass it around,"
      @beers -= 1
      puts "#{worded} bottle of beer on the wall.\n\n"
    end

    if @beers.to_i == 1
      puts "#{worded} bottle of beer on the wall,"
      puts "#{worded} bottle of beer,"
      puts "Take one down, pass it around,"
      puts "Zero bottles of beer on the wall.\n\n"
    end

    if @beers.to_i == 0
      print ""
    end

  end

end
classbeersong
属性存取器:啤酒
def初始化(啤酒)
如果啤酒>99
啤酒=99
elsif啤酒<1
啤酒=“零”
结束
@啤酒
结束
措辞明确
num_name={
90=>“九十”,80=>“八十”,70=>“七十”,60=>“六十”,
50=>“五十”,40=>“四十”,30=>“三十”,20=>“二十”,
19=>“十九”,18=>“十八”,17=>“十七”,16=>“十六”,
15=>“十五”,14=>“十四”,13=>“十三”,12=>“十二”,
11=>“十一”,10=>“十”,9=>“九”,8=>“八”,7=>“七”,
6=>“六”,5=>“五”,4=>“四”,3=>“三”,2=>“二”,1=>“一”
}
worded=“”
啤酒=@啤酒
斯普利特啤酒=啤酒到斯普利特
num_name.每个do|编号、名称|
拆分编号=编号到拆分编号
如果啤酒=数量
措辞为19&&split\u编号[0]。to\u i==split\u啤酒[0]。to\u i
措辞你的台词:

split_beers = beers.to_s.split
split_number = number.to_s.split
假设结果是一个数组,但事实并非如此,因为您没有提供一个用于拆分的模式 因此使用默认值。见:


最后是你想要的

我现在更新了整个班级。抱歉,请在问题中说明。请阅读此处,了解对该问题的不同看法。具体而言,什么不起作用?我理解,但今后请尽最大努力在该问题中适当地间隔和缩进。通过使代码更易于阅读和理解,它使每个人的生活更轻松。看看@Jordan当前的编辑,看看如何最好地实现这一点。
puts "10".to_s.split.inspect        # gives ==> ["10"]
puts "10".to_s.split('',2).inspect  # gives ==> ["1", "0"]