0

I use the following class definition

 class Workflow
    # class << self; attr_acessor :id, :name, :url  end
    # Please ignore the troubleshooting attemp above it didn't help, I got the ObjectNil

    def self.initialize ( id, name, url )
      @id = id
      @name = name
      @url = url
    end

    # @return [Object]
    def  self.list
      ret = Array.new
      wf = Hash.new
      vco = Vco.new
      vco.getAll.each do |link|
        link['attributes'].each do |attr|
          wf[ attr['name'] ] = attr[ 'value' ]
        end
        ret.push( self.new( wf[ 'id' ], wf[ 'name' ], wf[ 'itemHref' ]  ) )
      end
      return   ret
    end

used as:

  <% @workflows.each do |wf| %>
      <tr>
        <td><%= wf.id %></td>
        <td><%= wf.name %></td>
        <td><%= wf.url %></td>
      </tr>
  <% end %>

Thus the method Workflow.list should return an array of workflows. However, it doesn't do so as expected. When I use

        ret.push( self.new( wf[ 'id' ], wf[ 'name' ], wf[ 'itemHref' ]  ) )

I'm getting the "wrong number of arguments(3 for 0)" error. But when I use instead

       ret.push( self.initialize( wf[ 'id' ], wf[ 'name' ], wf[ 'itemHref' ]  ) )

the method list returns a list of url strings (it is the last assignment in the method, and should be that) What am I doing wrong?

2
  • What is initialise? Show the definition of it. Commented May 21, 2013 at 10:51
  • @sawa Sorry, "initialiSe" was a typo made during the post preparation. In real code there was initialiZe. Commented May 21, 2013 at 11:03

2 Answers 2

1

to remove that error on self.new call, change the def self.initialize ( id, name, url ) to def initialize ( id, name, url ). self.new tries to create a Workflow instance,by calling default initialize method, which don't take any arguments,but you are providing it arguments,which in turn throws error.

Sign up to request clarification or add additional context in comments.

10 Comments

Just tested it "def initialize ( id, name, url )" and "ret.push( self.new( wf[ 'id' ], wf[ 'name' ], wf[ 'itemHref' ] ) )" now getting error "undefined method `id' for #<Workflow:0x4006e90>"
@Vsevolod use as wf[@id]
:( "undefined method `[]' for #<Workflow:0x4573a58> -- but in my understanding instance variables in Ruby are the same as objct properties in other languages, or I#m speaking nonsense?
ue attr_acessor :id, :name, :url in the class.
@Priti : self.new tries to create a Workflow instance,by calling default initialize method, which don't take any arguments,but you are providing it arguments,which in turn throws error. - self.new will call the initialize method which is accepting 3 arguments.
|
0

First you've got def self.initialize which should be changed to def initialize since its a constructor. Constructor is a method that gets called when you do Class.new since you've defined the constructor as a class method you are getting the error.

Second the constructor is called initialize and you are making a call to initialise (typo?) if not fix that part as well.

1 Comment

Sorry, "initialiSe" was a typo made during the post preparation. In real code there was initialiZe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.