# Newbie problem of Class attributes

**URL:** <https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710>\
**Category:** Ruby\
**Created:** [October 10, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710 "2006-10-10T23:18:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 10, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/1 "2006-10-10T23:18:26Z")

</div>

I am trying to create a class level attribute which can read and write.  
I am doing something like this:

def self.selected\_client  
– my code –  
end

def self.selected\_client=(value)  
– my code –  
end

but its seems that the Ruby interpreter is not liking this. Can someone  
tell me how to create class level attributes that can read and write.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 10, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/2 "2006-10-10T23:25:13Z")

</div>

On 10/10/06, Dipesh B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but its seems that the Ruby interpreter is not liking this. Can someone  
> tell me how to create class level attributes that can read and write.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

class A  
def self.var() @@var end  
defl self.var=(v) @@var = v end  
end

A.var = 1  
A.var

If you try to access a class variable before it is set, the  
interpreter will throw a NameError at you.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 10, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/3 "2006-10-10T23:57:14Z")

</div>

On Oct 10, 2006, at 5:18 PM, Dipesh B. wrote:

> end
> 
> but its seems that the Ruby interpreter is not liking this. Can  
> someone  
> tell me how to create class level attributes that can read and write.

`
#! /usr/bin/env ruby -w
class A
class << self
attr_accessor :foo
end
end
A.foo = ‘bar’
A.foo # => “bar”
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 11, 2006, 12:21am UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/4 "2006-10-11T00:21:16Z")

</div>

On Oct 11, 6:56 am, Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def self.selected\_client=(value)  
> > attr\_accessor :foo  
> > end  
> > end
> 
> A.foo = ‘bar’  
> A.foo # =\> “bar”
> 
> Regards, Morton

What means ‘class \<\< self’ ? I don’t understand…

Plz anyone let me know secret ; )

Thanks.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 11, 2006, 12:29am UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/5 "2006-10-11T00:29:17Z")

</div>

On 10/10/06, Mait [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What means ‘class \<\< self’ ? I don’t understand…
> 
> Plz anyone let me know secret ; )
> 
> Thanks.

The ‘class \<\< self’ notation gives you access to the Singleton class  
for the object ‘self’.

There is a great explanation on the RubyGarden wiki page …

[http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial](http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial)

It is much better than my on line explanation.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 11, 2006, 1:01am UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/6 "2006-10-11T01:01:19Z")

</div>

On Oct 11, 7:28 am, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is a great explanation on the RubyGarden wiki page …
> 
> [http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial](http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial)
> 
> It is much better than my on line explanation.
> 
> Blessings,  
> TwP

Thanks Tim,

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 11, 2006, 3:17am UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/7 "2006-10-11T03:17:28Z")

</div>

Hi –

On Wed, 11 Oct 2006, Tim P. wrote:

> > end  
> > def self.var() @@var end  
> > defl self.var=(v) @@var = v end  
> > end
> 
> A.var = 1  
> A.var

I’d steer clear of class variables unless there’s some very compelling  
reason to use them. Classes can have regular attributes:

class C  
def self.x  
@x  
end  
def self.x=(value)  
@x = value  
end  
end

I realize Dipesh said “class level”, which could mean specifically  
using class variables, but then again, class variables aren’t really  
class level (they’re class/sub[sub[…]]class/instances-level).

David

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [October 11, 2006, 2:15am UTC](https://www.ruby-forum.com/t/newbie-problem-of-class-attributes/71710/8 "2006-10-11T02:15:24Z")

</div>

I’ve been trying to programmatically issue MSN Searches and processing  
the results. I’m having a hell of a time doing it and was wondering  
whether anyone had some coding or debugging advice.

First, I tried wsdl2ruby to generate some classes to work with, but it  
pukes:

C:\temp\>wsdl2ruby.rb --wsdl  
[http://soap.search.msn.com/webservices.asmx?wsdl](http://soap.search.msn.com/webservices.asmx?wsdl) --type client --force  
ignored element: {[http://www.w3.org/2001/XMLSchema](http://www.w3.org/2001/XMLSchema)}list  
ignored attr: {}default  
ignored attr: {[http://schemas.xmlsoap.org/ws/2004/08/addressing](http://schemas.xmlsoap.org/ws/2004/08/addressing)}Action  
I, [2006-10-10T16:36:52.259000 #2608] INFO – app: Creating class  
definition.  
W, [2006-10-10T16:36:52.259000 #2608] WARN – app: File ‘default.rb’  
exists but overrides it.  
F, [2006-10-10T16:36:52.275000 #2608] FATAL – app: Detected an  
exception. Stopping … incomplete simpleType (ArgumentError)  
C:/program files/ruby/lib/ruby/1.8/wsdl/xmlSchema/simpleType.rb:33:in  
`base’  
C:/program files/ruby/lib/ruby/1.8/wsdl/soap/classDefCreator.rb:217:in  
[snip]

(BTW, wsdl2ruby works with [http://api.google.com/GoogleSearch.wsdl.](http://api.google.com/GoogleSearch.wsdl.))

Second, I tried this code:

require ‘soap/wsdlDriver’  
wsdl\_url = ‘[http://soap.search.msn.com/webservices.asmx?wsdl](http://soap.search.msn.com/webservices.asmx?wsdl)’  
soap = SOAP::WSDLDriverFactory.new( wsdl\_url ).create\_rpc\_driver

msn\_params = { ‘AppID’ =\> ‘1064081Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’,  
‘Query’ =\> ‘ruby programming language’,  
‘CultureInfo’ =\> ‘en-US’,  
‘SafeSearch’ =\> ‘Strict’,  
‘Flags’ =\> ‘None’,  
‘Requests’ =\> {  
‘SourceRequest’ =\> {  
‘Source’ =\> ‘Web’,  
‘Offset’ =\> 0,  
‘Count’ =\> 10,  
‘ResultFields’ =\> ‘All’  
}  
}  
}

soap.search(:Request =\> msn\_params)

And got this:

irb(main):020:0\* soap.search(:Request =\> msn\_params)  
ArgumentError: incomplete simpleType from c:/Program F  
iles/ruby/lib/ruby/1.8/wsdl/xmlSchema/simpleType.rb:25:in  
`check_lexical_fo rmat' from c:/Program Files/ruby/lib/ruby/1.8/soap/mapping/wsdlliteralregistry.rb:113:in `simpleob  
j2soap’  
[snip]

Note: the Python equivalent of this code works just fine, so I think it  
has something to do with the way Ruby is processing SOAP.

Third, I tried to do it without SOAP:

require ‘rubygems’  
require ‘open-uri’  
require ‘rubyful\_soup’  
url =  
“[ruby programming language - Search](http://search.live.com/results.aspx?q=ruby+programming+language&mkt=en-)  
us&FORM=LVSP&go.x=0&go.y=0&go=Search”  
page = open(url)  
page\_content = page.read  
soup = BeautifulSoup.new(page\_content)

and I get this:

irb(main):007:0\> soup = BeautifulSoup.new(page\_content)  
ArgumentError: invalid value for Integer: “0183”  
from c:/Program  
Files/ruby/lib/ruby/gems/1.8/gems/htmltools-1.10/lib/html/sgml-parser.rb  
:335  
:in `Integer' from c:/Program Files/ruby/lib/ruby/gems/1.8/gems/htmltools-1.10/lib/html/sgml-parser.rb :335 :in `handle\_charref’  
from c:/Program  
Files/ruby/lib/ruby/gems/1.8/gems/htmltools-1.10/lib/html/sgml-parser.rb  
:159  
:in `goahead’

My next step is do to HTree/REXML, but I’d much rather use SOAP or  
BeautifulSoup to do this. Anyone got ideas?
