Hi!
I want to pass an array as a parameter to a method. But the values of it
inside the method are always Nil.
Can this be done?
···
--
Posted via http://www.ruby-forum.com/.
Hi!
I want to pass an array as a parameter to a method. But the values of it
inside the method are always Nil.
Can this be done?
--
Posted via http://www.ruby-forum.com/.
Could you show us the code which is doing this?
Farrel
On 15/11/06, Flaab Mrlinux <flaab_mrlinux@hotmail.com> wrote:
Hi!
I want to pass an array as a parameter to a method. But the values of it
inside the method are always Nil.Can this be done?
--
Posted via http://www.ruby-forum.com/\.
Farrel Lifson wrote:
Posted via http://www.ruby-forum.com/\.
Could you show us the code which is doing this?
Farrel
Yes. Here it is... The objetive of the program is the following:
- The user inserts a string of 15 signs.
- The program loads a .txt file with a bunch of strings of 15 signs
- The program compares each string of the file with the one the user
wrote, and returns the number of signs matching between ALL the strings
in the file with the one the user wrote, but only beggining at a minimum
amount of 10.
Here it goes
-------------------------------------------------------------------
####################################
#
# CLASES
#
####################################
# Definimos combinacion, subclase de array
class Combinacion < Array
@@quince = 0
@@catorce = 0
@@trece = 0
@@doce = 0
@@once = 0
@@diez = 0
def initialize(num)
@combi = Array.new(num)
end
# Devolver valores
def dame_resultados
print("De Quince: ", @@quince,"\n")
print("De Catorce: ", @@catorce,"\n")
print("De Trece: ", @@trece,"\n")
print("De Doce: ", @@doce,"\n")
print("De Once: ", @@once,"\n")
print("De Diez: ", @@diez,"\n")
end
# Comprobar los premios.
def comprobacion(combinacionTemp)
#Valor auxiliar de acumulador
@acum = 0
# Empezamos a comprobar. 15 Comprobaciones
0.upto(14) do |x|
if self == combinacionTemp
@acum = @acum + 1
elsif self == nil
@acum = @acum + 1
end
# Comprobamos premios e incrementamos variables de clase
if @acum == 10
@@diez = @@diez + 1
elsif @acum == 11
@@once = @@once +1
elsif @acum == 12
@@doce = @@doce +1
elsif @acum == 13
@@trece = @@trece +1
elsif @acum == 14
@@catorce = @@catorce+1
elsif @acum == 15
@@quince = @@quince+1
end # Fin de comprobacion
end
return self
end # Fin de comprobacion
end
# Definimos combi, sub clase de String
class Combi < String
# Metodo de pasar una cadena a un array.
def to_a(cadena)
# Definimos
@arrayc = Combinacion.new(15)
# Para cada elemento de la cadena
cadena.each_byte {
>c> # Construyo C
@arrayc.push(c.chr) # Lo convierto en char y almaceno en un
elemento de un array
}
return @arrayc
end
#####################################
#
# INICIO
#
#####################################
# Inicializamos nueva combinacionc
cganadora = Combinacion.new(15)
# Pedimos combinacion al usuario.15 Signos
0.upto(14) do |x| # Recibimos X como parametro contador
print("Partido ", x+1,": ")
char = gets
char.upcase!
# Insertamos en el array
cganadora = char
end # Fin de pedir combinaciones
# Definimos archivo
$CFILE = "combi.txt"
# Vamos a abrir archivo
File.open($CFILE) do |combi_file|
# Para cada linea
combi_file.each do |line|
combi = line
combinacionTemp = combi.to_a #Devuelve combinacionTemp y genera un
array
cganadora.comprobacion(combinacionTemp)
end
end
cganadora.dame_resultados
end
---------END --------------------------------------------------
--
Posted via http://www.ruby-forum.com/\.
Hi --
Farrel Lifson wrote:
Posted via http://www.ruby-forum.com/\.
Could you show us the code which is doing this?
Farrel
Yes. Here it is... The objetive of the program is the following:
- The user inserts a string of 15 signs.
- The program loads a .txt file with a bunch of strings of 15 signs
- The program compares each string of the file with the one the user
wrote, and returns the number of signs matching between ALL the strings
in the file with the one the user wrote, but only beggining at a minimum
amount of 10.Here it goes
[...]
combi_file.each do |line|
combi = linecombinacionTemp = combi.to_a #Devuelve combinacionTemp y genera un
array
cganadora.comprobacion(combinacionTemp)
When you to the to_a, you're going to end up with an array of one
element -- namely, the whole line. I think you want to split it up
into characters.
David
On Wed, 15 Nov 2006, Flaab Mrlinux wrote:
--
David A. Black | dblack@rubypal.com
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org