Forum: Ruby syntax error warning: useless use of a variable in void cont

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.
w3gat (Guest)
on 2005-11-23 22:25
(Received via mailing list)
class Tl
    def initialize(a,b)
        @a = a
        @b = b
    end

    def to_s
        "#{@a}    #{@b}"
    end

    def <=>(tlr)
        if @a > tlr.@a
            1
        elsif
            @a == tlr.@a
                0
        else
            -1
        end
    end

end

aa = Tl.new("tom","reilly")
ab = Tl.new("jack","charity")

r = aa <=> ab
p r

--------------------------
 >ruby -cw tmp.rb
tmp.rb:12: syntax error
tmp.rb:12: warning: useless use of a variable in void context
tmp.rb:14: syntax error
tmp.rb:15: syntax error
tmp.rb:15: warning: useless use of a variable in void context
tmp.rb:19: warning: else without rescue is useless
tmp.rb:22: syntax error
 >Exit code: 1


Considering I've been using Ruby for a couple of years, I've been
totally frustrated
making this bit of code work.

Help!
Thanks
leslie (Guest)
on 2005-11-23 22:41
(Received via mailing list)
Tom R. wrote:

>            1
> ab = Tl.new("jack","charity")
> tmp.rb:15: warning: useless use of a variable in void context
> Thanks
You seem to be having difficulty accessing the variables in the tla
object.
This seems to work:

#!/usr/bin/env ruby

class Tl
   attr_reader :a, :b

   def initialize(a,b)
       @a = a
       @b = b
   end

   def to_s
       "#{@a}    #{@b}"
   end

   def <=>(tlr)
       if @a > tlr.a
           1
       elsif
           @a == tlr.a
               0
       else
           -1
       end
   end
end

aa = Tl.new("tom","reilly")
ab = Tl.new("jack","charity")

r = aa <=> ab
p r
christophe.grandsire (Guest)
on 2005-11-23 23:01
(Received via mailing list)
Selon Tom R. :
> class Tl
>    def initialize(a,b)
>        @a = a
>        @b = b
>    end
>       def to_s
>        "#{@a}    #{@b}"
>    end
>       def <=>(tlr)
>        if @a > tlr.@a
                  ^^^^^^

>            1
>        elsif
>            @a == tlr.@a
                    ^^^^^^
> r = aa <=> ab
> tmp.rb:22: syntax error
>  >Exit code: 1
>
>
> Considering I've been using Ruby for a couple of years, I've been
> totally frustrated
> making this bit of code work.
>

Instance variables are completely private by default in Ruby, and you
can only make them accessible by adding accessor methods (what the
attr_* methods do for you). You can't access the private instance
variable of a different object, even if they have the same class, so
your "tlr.@a" simply won't work.

The only ways to make this work is:
- to make the instance variable @a readable with "attr_reader :a" and
replace "tlr.@a" with "tlr.a"
- to bypass encapsulation by replacing "tlr.@a" with
"tlr.instance_variable_get(:@a)".
--
Christophe G..

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.
ara.t.howard (Guest)
on 2005-11-23 23:25
(Received via mailing list)
On Thu, 24 Nov 2005, Tom R. wrote:

> class Tl
>   def initialize(a,b)
>       @a = a
>       @b = b
>   end
>      def to_s
>       "#{@a}    #{@b}"
>   end
>      def <=>(tlr)
>       if @a > tlr.@a
                     ^
                     ^
                     ^

                   you can't do this.

probably you want

   attr "a"
   ...

   tlr.a

hth.

-a
w3gat (Guest)
on 2005-11-23 23:54
(Received via mailing list)
Thanks everybody.
 I had thought that an @_whatever could be seen anywhere the class was
used.
TR
gavin (Guest)
on 2005-11-24 06:08
(Received via mailing list)
On Nov 23, 2005, at 3:57 PM, Christophe G. wrote:
> The only ways to make this work is:
> - to make the instance variable @a readable with "attr_reader :a"
> and replace "tlr.@a" with "tlr.a"
> - to bypass encapsulation by replacing "tlr.@a" with
> "tlr.instance_variable_get(:@a)".

To be pedantic:
a) attr_reader doesn't make an instance variable readable, it's a
convenient way to add a method that returns the value of the instance
variable.

b) There are other ways (variations on your latter) such as
tlr.instance_eval{ @a }
This topic is locked and can not be replied to.