So, I have this ruby file and I want to use it in my rails project, but I don’t have a clue on where or how to start, I’ve read about include and require, some sites tell me to use require, some to use include, and even to use both, but that’s it. I would like to know where to put the necessary code, how to use the file’s methods and where to put the file in the project directory, because I want to call it from a view but I don’t know if that is the best, I’m sorry if this is a dumb question but rails is still new for me. I appreciate all the help you can offer. Thanks for your time.
The file I’m trying to use is the one that converts numbers to words created by Faustino Vasquez limon and it’s a full class:
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
That is what i want to use from my app. Thank you for your time.
Since it’s your own class and you want to use it in one of the Rails views, there’s many ways of using that code.
My own way and you may or may not choose to do it this way would be to include that file in the
libdirectory and thenrequireit in the helper file of the you view you want to include it in.This would allow you to access methods and initializers from that file and you would be able to create methods in your helpers to use on your views.