# \[METHODS\] Var types as parameters

**URL:** <https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031>\
**Category:** Ruby\
**Created:** [November 15, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031 "2006-11-15T13:26:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031/1 "2006-11-15T13:26:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031/2 "2006-11-15T13:35:45Z")

</div>

On 15/11/06, Flaab M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).

Could you show us the code which is doing this?

Farrel

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031/3 "2006-11-15T14:11:19Z")

</div>

Farrel L. wrote:

> > Posted via [http://www.ruby-forum.com/](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[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 --------------------------------------------------

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 15, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/methods-var-types-as-parameters/76031/4 "2006-11-15T14:25:54Z")

</div>

Hi –

On Wed, 15 Nov 2006, Flaab M. wrote:

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