Acceder a variables de clase


#1

Hola a todos,

llevo un tiempo peleandome con un error. Tiene pinta de basico asi que
se lo pongo aqui porque seguro que mucha gente puede resolverlo:

Tengo una clase Target:

class Target

attr_writer :hit_sequence_accession,
:hit_sequence_desc,
:hit_sequence_ID

attr_reader :hit_sequence_accession,
:hit_sequence_desc,
:hit_sequence_ID

def initialize(hit_sequence_accession,
hit_sequence_desc,
hit_sequence_ID)
@hit_sequence_accession = hit_sequence_accession
@hit_sequence_desc = hit_sequence_desc
@hit_sequence_ID = hit_sequence_ID
end

end

A la que le paso los valores 0, 1 y 0 mediante una consulta. Luego hago:

p @targets
p @targets[0].hit_sequence_ID
p @targets[0].hit_sequence_desc
p @targets[0].hit_sequence_accession

y esto es lo que me devuelve la consola:

[#<Target:0x48764c4 @attributes={“hit_sequence_desc”=>“0”,
“hit_sequence_ID”=>"1
", “hit_sequence_accession”=>“0”}>]
nil
nil
nil

O sea, a traves de la llamada a @targets puedo ver los valores y los
nombres de los atributos, mientras que llamando a los atributos uno a
uno me devuelve nil.

Alguien sabe que estoy haciendo mal aqui??


#2

Sorry I though I was on the Spanish forum.

There it goes the english translation:

I have a Target class:

class Target

attr_writer :hit_sequence_accession,
:hit_sequence_desc,
:hit_sequence_ID

attr_reader :hit_sequence_accession,
:hit_sequence_desc,
:hit_sequence_ID

def initialize(hit_sequence_accession,
hit_sequence_desc,
hit_sequence_ID)
@hit_sequence_accession = hit_sequence_accession
@hit_sequence_desc = hit_sequence_desc
@hit_sequence_ID = hit_sequence_ID
end

end

To which I pass the values 0, 1 and 0 with and SQL sentence. Then I do:

p @targets
p @targets[0].hit_sequence_ID
p @targets[0].hit_sequence_desc
p @targets[0].hit_sequence_accession

and this is what I get:

[#<Target:0x48764c4 @attributes={“hit_sequence_desc”=>“0”,
“hit_sequence_ID”=>"1
", “hit_sequence_accession”=>“0”}>]
nil
nil
nil

Calling the @targets collection directly gives me the correct values,
while calling the attributes returns nil values.

Anyone knows what I’m doing wrong here??

Thank you