ruby on rails - How do I split a string into three parts? -


i have string "001-1776591-7", , want divide 3 parts, "-" being split parameter.

i have created 2 methods, first , last, second part of string, how can that?


more info:

i created 2 methods in class, when loading view error, details below:

  def serie       @serie || cedula.to_s.split('-').[0] : @serie   end    def identificador       @identificador || cedula.to_s.split('-').[1] : @identificador  end   def verificador       @verificador || cedula.to_s.split('-').[2] : @verificador  end   syntaxerror in tecnicoscontroller#index  /home/lurraca/desktop/rails_project/arlink/app/models/tecnico.rb:7: syntax error, unexpected '['           @serie || cedula.to_s.split('-').[0] : @serie                                            ^ /home/lurraca/desktop/rails_project/arlink/app/models/tecnico.rb:11: syntax error, unexpected '[' ...dor || cedula.to_s.split('-').[1] : @identificador ...                              ^ /home/lurraca/desktop/rails_project/arlink/app/models/tecnico.rb:15: syntax error, unexpected '['           @verificador || cedula.to_s.split('-').[2] : @verificador 

the split method returns array, can access second element of same way second element of other array: array[1]. also, using || bar can make code simpler. try this:

def serie    @serie || cedula.to_s.split('-')[0] end  def banana    @banana || cedula.to_s.split('-')[1] end  def verificador    @verificador || cedula.to_s.split('-')[2] end 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -