Newbie problem of Class attributes

D

Dipesh Batheja

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

Tim Pease

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.

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
 
M

Morton Goldberg

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.

<code>
#! /usr/bin/env ruby -w

class A
class << self
attr_accessor :foo
end
end

A.foo = 'bar'
A.foo # => "bar"
</code>

Regards, Morton
 
M

Mait

#! /usr/bin/env ruby -w

class A
class << self
attr_accessor :foo
end
end

A.foo = 'bar'
A.foo # => "bar"
</code>

Regards, Morton

What means 'class << self' ? I don't understand...

Plz anyone let me know secret ; )

Thanks.
 
J

Justin Chan

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. =20

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 --type client --force
ignored element: {http://www.w3.org/2001/XMLSchema}list
ignored attr: {}default
ignored attr: {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=20
[snip]

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

Second, I tried this code:

require 'soap/wsdlDriver'
wsdl_url =3D 'http://soap.search.msn.com/webservices.asmx?wsdl'
soap =3D SOAP::WSDLDriverFactory.new( wsdl_url ).create_rpc_driver

msn_params =3D { 'AppID' =3D> '1064081Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'Query' =3D> 'ruby programming language',
'CultureInfo' =3D> 'en-US',
'SafeSearch' =3D> 'Strict',
'Flags' =3D> 'None',
'Requests' =3D> {
'SourceRequest' =3D> { =20
'Source' =3D> 'Web',
'Offset' =3D> 0,
'Count' =3D> 10,
'ResultFields' =3D> 'All'
}
}
}
=20
soap.search:)Request =3D> msn_params)

And got this:

irb(main):020:0* soap.search:)Request =3D> 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 =3D
"http://search.live.com/results.aspx?q=3Druby+programming+language&mkt=3D=
en-
us&FORM=3DLVSP&go.x=3D0&go.y=3D0&go=3DSearch"
page =3D open(url)
page_content =3D page.read
soup =3D BeautifulSoup.new(page_content)

and I get this:

irb(main):007:0> soup =3D 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?
 
D

dblack

Hi --

class A
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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,708
Latest member
SharonMaes

Latest Threads

Top