newbie problem with Ruby

A

Alex Gd

Hi there,

just getting to know the language by writing console twitter client, I'm
getting this error:

ArgumentError: wrong number of arguments (1 for 0)
from ./twitter_client.rb:7:in `initialize'
from ./twitter_client.rb:7:in `new'
from ./twitter_client.rb:7
from (irb):1:in `require'
from (irb):1

When trying to load the code below.

class TwitterMethod
Statuses = TwitterMethod.new("statuses/show")

attr_reader :credentials
attr_writer :credentials

TwitterHome = "http://www.twitter.com/"


def initialize(methodLocation)
@methodUrlString = methodLocation
end

public
def call()
response
Net::HTTP.start(TwitterHome).start{|http|
request = Net::HTTP::Get.new(@methodUrlString)
@credentials.sign(request)
response = http.request(request)
}
return JSON.parse(response)
end

end



I can't work out what is going wrong here, I am guessing that its
something to do with the class not being initialized by the time I call
TwitterMethod.new to set the class constant. I also don't know if this
is a particularly rubyish way to do this, please let me know if theres a
better way.

Cheers
Alex
 
J

Jesús Gabriel y Galán

Hi there,

just getting to know the language by writing console twitter client, I'm
getting this error:

=A0 =A0 =A0 =A0ArgumentError: wrong number of arguments (1 for 0)
=A0from ./twitter_client.rb:7:in `initialize'
=A0from ./twitter_client.rb:7:in `new'
=A0from ./twitter_client.rb:7
=A0from (irb):1:in `require'
=A0from (irb):1

When trying to load the code below.

class TwitterMethod
=A0Statuses =3D TwitterMethod.new("statuses/show")

=A0attr_reader :credentials
=A0attr_writer :credentials

=A0TwitterHome =3D "http://www.twitter.com/"


=A0def initialize(methodLocation)
=A0 =A0@methodUrlString =3D methodLocation
=A0end

public
=A0def call()
=A0 =A0response
=A0 =A0Net::HTTP.start(TwitterHome).start{|http|
=A0 =A0 =A0request =3D Net::HTTP::Get.new(@methodUrlString)
=A0 =A0 [email protected](request)
=A0 =A0 =A0response =3D http.request(request)
=A0 =A0}
=A0 =A0return JSON.parse(response)
=A0end

end



I can't work out what is going wrong here, I am guessing that its
something to do with the class not being initialized by the time I call
TwitterMethod.new to set the class constant. I also don't know if this
is a particularly rubyish way to do this, please let me know if theres a
better way.

By default, the initializer of a class has no arguments. You are
calling TwitterMethod.new (which ends up calling initialize) with an
argument, instead of 0, before overriding the initialize method.

I don't know what you mean to do with the Statuses constant, so I'm
not sure if this is the best way to achieve it. But if you just want
to get rid of the error, put the initialize method before the call the
TwitterMethod.new.

Also, attr_accessor does the same as reader and writer.

Jesus.
 
J

Jesús Gabriel y Galán

2009/10/11 Jes=FAs Gabriel y Gal=E1n said:
By default, the initializer of a class has no arguments.

Really bad explanation !!!!!!!!!!

If your class doesn't inherit from another class, it inherits directly
from Object:

irb(main):011:0> class A
irb(main):012:1> end
=3D> nil
irb(main):013:0> A.ancestors
=3D> [A, Object, Kernel]

And Object#initialize takes no arguments. That's why your class'
initialize method takes no argument before overriding it.

Jesus.
 
A

Alex Gd

Thanks guys, thats sorted it out. I didn't realize that the initialize()
override had to occur before instantiating a class. I guess I'm too used
to java :)

What I was trying to acheive with the 'Status' constant was something
similar to a java enum I suppose. The intention was to have a private
constructor with a class Constant for each twitter method.

Cheers
Alex
 

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
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top