Instance variable exists?

Hi,

what is the preferred way to see if an object already includes a
instance variable?

for now i do something like:

@my_object.instance_variables.include? "var_name"

Cheers
detlef

Hi,

AFAIK it's the only way.

Question: Why does Object#instance_varialbe_get have no documentation?
It's behaviour isn't that clear in cases like

···

On 2/18/07, Detlef Reichl <detlef.reichl@gmx.org> wrote:

Hi,

what is the preferred way to see if an object already includes a
instance variable?

for now i do something like:

@my_object.instance_variables.include? "var_name"

Cheers
detlef

Hi Detief, Aur,

> Hi,
>
> what is the preferred way to see if an object already includes a
> instance variable?
>
> for now i do something like:
>
> @my_object.instance_variables.include? "var_name"
>
Hi,

AFAIK it's the only way.

There's also:

  obj.instance_eval{defined?(@foo)}

As you're hopefully aware, though, it's probably not particularly
straightforward because it's discouraged in general. Peeking at an
object's instance variables is kinda intruding on its privacy. There
are some valid uses to be sure, but usually I think you're better off
providing accessors for anything of interest outside the object.

Question: Why does Object#instance_varialbe_get have no documentation?
It's behaviour isn't that clear in cases like

Where are you looking? "ri instance_variable_get" gives me docs.

Regards,
George.

···

On 2/19/07, SonOfLilit <sonoflilit@gmail.com> wrote:

On 2/18/07, Detlef Reichl <detlef.reichl@gmx.org> wrote:

aur-sarafs-computer:~ aursaraf$ ri Object#instance_variable_get -T
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)

···

------------------------------------------------------------------------
     (no description...)
aur-sarafs-computer:~ aursaraf$ ruby -v
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-darwin8.8.1]

On 2/18/07, George Ogata <george.ogata@gmail.com> wrote:

Hi Detief, Aur,

On 2/19/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> On 2/18/07, Detlef Reichl <detlef.reichl@gmx.org> wrote:
> > Hi,
> >
> > what is the preferred way to see if an object already includes a
> > instance variable?
> >
> > for now i do something like:
> >
> > @my_object.instance_variables.include? "var_name"
> >
> Hi,
>
> AFAIK it's the only way.

There's also:

  obj.instance_eval{defined?(@foo)}

As you're hopefully aware, though, it's probably not particularly
straightforward because it's discouraged in general. Peeking at an
object's instance variables is kinda intruding on its privacy. There
are some valid uses to be sure, but usually I think you're better off
providing accessors for anything of interest outside the object.

> Question: Why does Object#instance_varialbe_get have no documentation?
> It's behaviour isn't that clear in cases like

Where are you looking? "ri instance_variable_get" gives me docs.

Regards,
George.

g@bang:~$ ri -T instance_variable_get
------------------------------------------- Object#instance_variable_get
     obj.instance_variable_get(symbol) => obj

···

On 2/19/07, SonOfLilit <sonoflilit@gmail.com> wrote:

aur-sarafs-computer:~ aursaraf$ ri Object#instance_variable_get -T
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)
------------------------------------------------------------------------
     (no description...)
aur-sarafs-computer:~ aursaraf$ ruby -v
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-darwin8.8.1]

------------------------------------------------------------------------
     Returns the value of the given instance variable (or throws a
     NameError exception). The @ part of the variable name should be
     included for regular instance variables

        class Fred
          def initialize(p1, p2)
            @a, @b = p1, p2
          end
        end
        fred = Fred.new('cat', 99)
        fred.instance_variable_get(:@a) #=> "cat"
        fred.instance_variable_get("@b") #=> 99

g@bang:~$ ruby -v
ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]

Maybe a version thing? (Just guessing.)

Regards,
George.

FYI I get the same thing.
E:\Documents and Settings\Jason>ri instance_variable_get
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)

···

On 2/18/07, SonOfLilit <sonoflilit@gmail.com> wrote:

aur-sarafs-computer:~ aursaraf$ ri Object#instance_variable_get -T
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)
------------------------------------------------------------------------
     (no description...)
aur-sarafs-computer:~ aursaraf$ ruby -v
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-darwin8.8.1]

> As you're hopefully aware, though, it's probably not particularly
> straightforward because it's discouraged in general. Peeking at an
> object's instance variables is kinda intruding on its privacy. There
> are some valid uses to be sure, but usually I think you're better off
> providing accessors for anything of interest outside the object.
>
> > Question: Why does Object#instance_varialbe_get have no documentation?
> > It's behaviour isn't that clear in cases like
>
> Where are you looking? "ri instance_variable_get" gives me docs.

------------------------------------------------------------------------
     (no description...)

E:\Documents and Settings\Jason>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

documentation here:
http://www.ruby-doc.org/core/classes/Object.src/M000323.html

Rdoc is there !!

ri instance_variable_get

------------------------------------------- Object#instance_variable_get
     obj.instance_variable_get(symbol) => obj

···

------------------------------------------------------------------------
     Returns the value of the given instance variable (or throws a
     +NameError+ exception). The +@+ part of the variable name should be
     included for regular instance variables

        class Fred
          def initialize(p1, p2)
            @a, @b = p1, p2
          end
        end
        fred = Fred.new('cat', 99)
        fred.instance_variable_get(:@a) #=> "cat"
        fred.instance_variable_get("@b") #=> 99

On 2/18/07, Jason Mayer <slamboy@gmail.com> wrote:

On 2/18/07, SonOfLilit <sonoflilit@gmail.com> wrote:
>
> aur-sarafs-computer:~ aursaraf$ ri Object#instance_variable_get -T
> ------------------------------------------- Object#instance_variable_get
> instance_variable_get(ivarname)
> ------------------------------------------------------------------------
> (no description...)
> aur-sarafs-computer:~ aursaraf$ ruby -v
> ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-darwin8.8.1]
>
> > As you're hopefully aware, though, it's probably not particularly
> > straightforward because it's discouraged in general. Peeking at an
> > object's instance variables is kinda intruding on its privacy. There
> > are some valid uses to be sure, but usually I think you're better off
> > providing accessors for anything of interest outside the object.
> >
> > > Question: Why does Object#instance_varialbe_get have no
documentation?
> > > It's behaviour isn't that clear in cases like
> >
> > Where are you looking? "ri instance_variable_get" gives me docs.

FYI I get the same thing.
E:\Documents and Settings\Jason>ri instance_variable_get
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)
------------------------------------------------------------------------
     (no description...)

E:\Documents and Settings\Jason>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

documentation here:
http://www.ruby-doc.org/core/classes/Object.src/M000323.html

--
sur
http://expressica.com

FYI I get the same thing.
E:\Documents and Settings\Jason>ri instance_variable_get
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)
------------------------------------------------------------------------
     (no description...)

E:\Documents and Settings\Jason>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

same here:

<giles-computer:giles> [02-18 08:00] ~
! ri Object#instance_variable_get -T
------------------------------------------- Object#instance_variable_get
     instance_variable_get(ivarname)

···

------------------------------------------------------------------------
     (no description...)
<giles-computer:giles> [02-18 08:00] ~
! ruby -v
ruby 1.8.5 (2006-08-25) [i686-darwin8.8.3]

haven't had any trouble with ri on this machine before, that I remember.

--
Giles Bowkett
http://www.gilesgoatboy.org

http://gilesgoatboy.blogspot.com