# Newbie problem with Ruby

**URL:** <https://rubytalk.org/t/newbie-problem-with-ruby/55761>\
**Category:** ruby-talk\
**Created:** [11 October 2009 21:03 UTC](https://rubytalk.org/t/newbie-problem-with-ruby/55761 "2009-10-11T21:03:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Gd](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@Alex\_Gd](https://rubytalk.org/u/Alex_Gd)\
**Post date:** [11 October 2009 21:03 UTC](https://rubytalk.org/t/newbie-problem-with-ruby/55761/1 "2009-10-11T21:03:12Z")

</div>

Hi there,

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;from ./twitter\_client.rb:7:in `initialize'  
&nbsp;&nbsp;from ./twitter\_client.rb:7:in `new'  
&nbsp;&nbsp;from ./twitter\_client.rb:7  
&nbsp;&nbsp;from (irb):1:in `require'  
&nbsp;&nbsp;from (irb):1

When trying to load the code below.

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

&nbsp;&nbsp;attr\_reader :credentials  
&nbsp;&nbsp;attr\_writer :credentials

&nbsp;&nbsp;TwitterHome = "[http://www.twitter.com/](http://www.twitter.com/)"

&nbsp;&nbsp;def initialize(methodLocation)  
&nbsp;&nbsp;&nbsp;&nbsp;@methodUrlString = methodLocation  
&nbsp;&nbsp;end

public  
&nbsp;&nbsp;def call()  
&nbsp;&nbsp;&nbsp;&nbsp;response  
&nbsp;&nbsp;&nbsp;&nbsp;Net::HTTP.start(TwitterHome).start{|http|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request = Net::HTTP::Get.new(@methodUrlString)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@credentials.sign(request)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = http.request(request)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;return JSON.parse(response)  
&nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [11 October 2009 21:57 UTC](https://rubytalk.org/t/newbie-problem-with-ruby/55761/2 "2009-10-11T21:57:55Z")

</div>

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.

> **···**
>
> On Sun, Oct 11, 2009 at 11:03 PM, Alex Gd \<quixoptic@gmail.com\> wrote:
> 
> > 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/&quot](http://www.twitter.com/&quot);
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [11 October 2009 22:01 UTC](https://rubytalk.org/t/newbie-problem-with-ruby/55761/3 "2009-10-11T22:01:41Z")

</div>

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  
=\> nil  
irb(main):013:0\> A.ancestors  
=\> [A, Object, Kernel]

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

Jesus.

> **···**
>
> 2009/10/11 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> 
> > By default, the initializer of a class has no arguments.

---

<div class="post-metadata">

**Author:** ![Alex\_Gd](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@Alex\_Gd](https://rubytalk.org/u/Alex_Gd)\
**Post date:** [11 October 2009 23:15 UTC](https://rubytalk.org/t/newbie-problem-with-ruby/55761/4 "2009-10-11T23:15:32Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
