Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
[METHODS] Var types as parameters
Flaab Mrlinux
11/15/2006 12:26:00 PM
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-...
.
3 Answers
Farrel Lifson
11/15/2006 12:34:00 PM
0
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-...
.
Could you show us the code which is doing this?
Farrel
Flaab Mrlinux
11/15/2006 1:11:00 PM
0
Farrel Lifson wrote:
>> Posted via
http://www.ruby-...
.
>
> 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 --------------------------------------------------
--
Posted via
http://www.ruby-...
.
dblack
11/15/2006 1:26:00 PM
0
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
[METHODS] Var types as parameters
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password