I am using Ruby console. I start it by using Rails console and then do require ‘path to my file’ which works fine.
The file looks like this:
module App
module Tools
module Pollers
class Kpi
attr_reader :start_time,:stop_time
def initialize(start_time,stop_time)
@start_time = start_time
@stop_time = stop_time
end
def create_social_audiences
....
So what I do is declare the stop and start time like this in the console
var end_date = new Date(2012, 7, 1);
var start = new Date(2012, 5, 1);
and then I try to call .new on this file and get this error
>> kpi = App::Tools::Pollers::Kpi.new
ArgumentError: wrong number of arguments (0 for 2)
from (irb):7:in `initialize'
from (irb):7:in `new'
from (irb):7
>>
but the weird part is that if I do this command which attempts to pass the variables it works:
>> kpi = App::Tools::Pollers::Kpi.new(start , end_date)
=> #<App::Tools::Pollers::Kpi:0x11489e198 @start_time=nil, @stop_time=nil>
but then when I try to do set the varialbes to anything other than nil I get errors that initialize is a private method:
?> kpi.initialize(start, end_date)
NoMethodError: private method `initialize' called for #<App::Tools::Pollers::Kpi:0x11489e198>
from (irb):15
Any thoughts on what might be happening wrong here?
Thanks!!
You should not call the class initializer after the class has been instantiated (and you cannot from outside of the class itself, given that it has private visibility).
The following does not work because you have an initializer with two parameters, therefore you also must pass those:
This works because the initializer is called when the class is being constructed (the
initializemethod is always invoked when you create a new instance usingnew):Finally, the following does not work (no matter what values start/end_date hold) because, as I explained earlier, the initializer of a class is private in ruby:
If you want to modify these variables after constructing the class, create a method for that: