所以,我有这个 ruby 文件,我想在我的 rails 项目中使用它,但我不知道从哪里开始或如何开始,我已经阅读了 include 和 require,一些网站告诉我使用 require,一些使用包括,甚至使用两者,但仅此而已。我想知道在哪里放置必要的代码,如何使用文件的方法以及将文件放在项目目录中的哪里,因为我想从视图中调用它但我不知道这是否是最好的,如果这是一个愚蠢的问题,我很抱歉,但 rails 对我来说仍然是新的。感谢您提供的所有帮助。谢谢你的时间。
我正在尝试使用的文件是将数字转换为由 Faustino Vasquez limon 创建的单词的文件,它是一个完整的类:
numeros.rb
class Numlet
def initialize(numero)
@numero = numero.to_s.reverse.split("")
@i = 0
@j = 0
@parte1 = []
@parte2 = []
@especial = ""
@numlet = []
@bandera=0
@bandera1=0
@a =[["Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve"],
["Diez","Veinte","Treinta","Cuarenta","Cincuenta","Sesenta","Setenta","Ochenta","Noventa"],
["Ciento","Doscientos","Trescientos","Cuatrocientos","Quinientos","Seiscientos","Setecientos","Ochocientos","Novecientos"]]
end
def especial
@numlet[@j] = case @especial
when "11"then "Once"
when "12"then "Doce"
when "13"then "Trece"
when "14"then "Catorce"
when "15"then "Quice"
when "16"then "Dieciseis"
when "17"then "Diecisiete"
when "18"then "Dieciocho"
when "19"then "Diecinueve"
when "21"then "Veintiun"
when "22"then "Veintidos"
when "23"then "Veintitres"
when "24"then "Veinticuatro"
when "25"then "Veinticinco"
when "26"then "Veintiseis"
when "27"then "Veintisite"
when "28"then "Veintiocho"
when "29"then "Veintinueve"
else return 0
end
end
def repetir
case @numero.length
when 0..3 then @parte1[0] = @numero[0..@numero.length]
when 4..6 then @parte1[0] = @numero[0..2];@parte1[1] = @numero[3..@numero.length]
when 7..9 then @parte1[0] = @numero[0..2];@parte1[1] = @numero[3..5]; @parte1[2] = @numero[6..@numero.length]
else return 0
end
end
def convierte
@bandera1=0
@i=0
case @bandera
when 1 then @numlet[@j]="mil";@j+=1
when 2 then (@parte2.length==1 and @parte2[0]==1) ? @numlet[@j]="millon" : @numlet[@j]="millones";@j+=1
end
@especial = [@parte2[@i+1],@parte2[@i]].to_s
if especial != 0
@i+=2
@j+=1
else
if @parte2[@i].to_s =="1"
@numlet[@j]="Un"
@i+=1
@j+=1
end
end
while @i < @parte2.length
if @parte2[@i].to_i ==0
@i+=1
@bandera1+=1
else
if @parte2.length != 1 and @bandera1 ==0
if @i == 1
@numlet[@j]="y"
@j+=1
end
end
@numlet[@j] = @a[@i][@parte2[@i].to_i-1]
if @i == 2 and @bandera1==2 and @numlet[@j]=="Ciento"
@numlet[@j]="Cien"
end
@j+=1
@i+=1
end
end
@bandera+=1
end
def termina
@numlet.reverse.join(" ")
end
def a_letra
if repetir != 0
@parte1.each do |@parte2|
convierte
end
print "#{termina}\n"
else
print "Este numero no puede ser convertido\n"
end
end
end
这就是我想从我的应用程序中使用的。感谢您的时间。