2013-02-11 75 views
0

所以,我有这个红宝石文件,我想在我的rails项目中使用它,但我不知道在哪里或如何开始,我已阅读包括和要求,有些网站告诉我使用要求,有些使用include,甚至使用两者,但就是这样。我想知道在哪里放置必要的代码,如何使用文件的方法以及将文件放在项目目录中的位置,因为我想从视图中调用它,但我不知道这是否是最好的,我很抱歉,如果这是一个愚蠢的问题,但铁轨对我来说仍然是新的。我感谢您提供的所有帮助。谢谢你的时间。如何在导轨3上使用“外部”类?

我想使用的文件是将数字转换到由福斯蒂诺·巴斯克斯利蒙创建字之一,它是一个完整的类:

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[[email protected]] 
    when 4..6 then @parte1[0] = @numero[0..2];@parte1[1] = @numero[[email protected]] 
    when 7..9 then @parte1[0] = @numero[0..2];@parte1[1] = @numero[3..5]; @parte1[2] = @numero[[email protected]] 
    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 

那是什么我想从我的应用程序中使用。感谢您的时间。

+0

这真的取决于你的ruby文件在做什么......如果它只是一些辅助方法,那么你可以将它包含在app/helpers目录中并包含它。如果它是一个完整的库,那么它将在lib /下,并且你需要有某种初始化器。发布代码片段,我们将能够更好地为您提供帮助。 – 2013-02-11 16:04:06

+0

添加了感谢代码。 – Jorge 2013-02-11 17:45:30

回答

0

由于它是您自己的类,并且您希望在Rails视图之一中使用它,因此有很多方法可以使用该代码。

我自己的方式,你可能会或可能不会选择做这种方式应该包含在lib目录,然后将该文件require它的查看您要包括它在帮助文件。

这将允许您从该文件访问方法和初始值设定项,并且您可以在助手中创建方法以在视图上使用。

+0

谢谢Leo!有效!我实际上首先尝试将要求放在控制器和模型上,它也起作用!谢谢你的时间和答案! – Jorge 2013-02-11 22:15:35

+0

没问题,不要忘了投票并选择答案,当你可以:) – 2013-02-11 22:33:55