Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > syntax error warning: useless use of a variable in void context

Reply
Thread Tools

syntax error warning: useless use of a variable in void context

 
 
Tom Reilly
Guest
Posts: n/a
 
      11-23-2005
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


 
Reply With Quote
 
 
 
 
Leslie Viljoen
Guest
Posts: n/a
 
      11-23-2005
Tom Reilly wrote:

> 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


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



 
Reply With Quote
 
 
 
 
Christophe Grandsire
Guest
Posts: n/a
 
      11-23-2005
Selon Tom Reilly :
> 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.
>


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

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.


 
Reply With Quote
 
Ara.T.Howard
Guest
Posts: n/a
 
      11-23-2005
On Thu, 24 Nov 2005, Tom Reilly 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
--
================================================== =============================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
================================================== =============================



 
Reply With Quote
 
Tom Reilly
Guest
Posts: n/a
 
      11-23-2005
Thanks everybody.
I had thought that an @_whatever could be seen anywhere the class was used.
TR


 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      11-24-2005
On Nov 23, 2005, at 3:57 PM, Christophe Grandsire 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 }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not sure why I am getting the following warning "Useless use of aconstant in void context" mcardeiro@yahoo.com Perl Misc 7 12-15-2007 09:15 PM
Useless use of array element in void context Marek Perl Misc 24 06-16-2007 02:39 PM
Useless use of a constant in void context makko Perl Misc 2 09-19-2005 01:18 PM
Useless use of private variable in void context at parser.cgi line 48. jon rogers Perl Misc 3 12-04-2003 12:21 AM
Useless use of private variable in void context ... ? George R. Gonzalez Perl Misc 1 08-08-2003 06:47 PM



Advertisments