Forum: Ruby on Rails Acceder a variables de clase

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alvaro P. (Guest)
on 2007-07-25 21:21
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??
Alvaro P. (Guest)
on 2007-07-25 21:26
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
This topic is locked and can not be replied to.