[METHODS] Var types as parameters

F

Flaab Mrlinux

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?
 
F

Farrel Lifson

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?

Could you show us the code which is doing this?

Farrel
 
F

Flaab Mrlinux

Farrel said:
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[x] == combinacionTemp[x]
@acum = @acum + 1
elsif self[x] == 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[x] = 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 --------------------------------------------------
 
D

dblack

Hi --

Farrel said:
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 = line

combinacionTemp = 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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,727
Latest member
PasqualePf

Latest Threads

Top