0
class Generic
  
  @@router=''
  @@digit=''
  @@data=''
  def initialize(str)
    @str=str
  end
  #def seperate
  #  @str="434 <user;password>##ROUTER=BROKER_NAME##"
    #puts @str.split()
   def digit
    @digit=@str[0,3]
    puts "Digit Part: " +@digit
  end
  def data
    @data=@str[/<[a-z]*;[a-z]*>/]
    puts "Data Part: " +@data
  end
  def router  
    @router=@str[/##[A-Z]*=[A-Z]*_[A-Z]*##/]
    puts "Router part: " +@router
 end
end

class Sub < Generic
  
end
   




genericobj=Generic.new('434 <user;password>##ROUTER=BROKER_NAME##')
genericobj.data
genericobj.digit
genericobj.router
subobj=Sub.new
subobj.data

Here when I try to create a new subclass object I get the following error
"home/user/workspace/Parser/generic.rb:38:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
from /home/user/workspace/Parser/generic.rb:38:in `new'
from /home/user/workspace/Parser/generic.rb:38

Am not trying to use a constructor for the subclass but I still get this error. Please someone tell me where am going wrong

Thanks in Advance

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by L7Sqr
0

Well, you define the class initializer ( def initialize(str) ) to take an argument in the base class. You do not change this behavior in the derived class. You can do one of the following to solve your problem:

class Sub < Generic
   def initialize
      @str = ""
   end
end

Which defined a new initializer method in the derived class.

class Generic
   def initialize(str="")
      @str=str
   end
   # ...

Which assigns a default value if no value is provided
Or

subobj=Sub.new ""

Provide an empty value for the object you are trying to create.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.