0

I'm porting some code from other language to Ruby, in my Class I need to maintain compatibility with those 4 constructors:

def initialize(b)
def initialize(a, b)
def initialize(b, c)
def initialize(a, b, c)

I was trying this way but it does not work:

def initialize(a="", b, c="")

Is there a smart solution? I cannot use hash.

5
  • 1
    What is the difference between initialize(a, b) and initialize(b, c)? Commented Jun 22, 2012 at 16:37
  • a, b and c are of completely different types. Commented Jun 22, 2012 at 16:48
  • 3
    Converting code from a statically to dynamically typed language you're either going to have to fundamentally alter some things or implement very ugly, un-Ruby workarounds. Commented Jun 22, 2012 at 16:50
  • It doesn't make sense to me that you want to convert to Ruby but use the conventions of the language you're migrating from. Commented Jun 22, 2012 at 16:55
  • Maybe it doesn't make sense to port to Ruby at all. If I can find some rules, then I can automate porting. Is there any tool to do this? Commented Jun 22, 2012 at 17:03

3 Answers 3

2

If you can't use a hash (why not?), and they must be initialize methods, you're likely out of luck. Without typing or positional information, there's just no way to destructure, say, an array, and determine which argument is which.

IMO the cleanest would be to provide class methods that do the construction and accept the arguments specific to the constructor (factory, in this case).

If the arguments are of different types you could use that information to determine which values are being passed in, I suppose, but IMO that gets pretty ugly pretty fast.

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

2 Comments

I cannot use hash because I dont want to rewrite a great # of call to this method. I'm porting code, I'm trying to find the easiest way.
The arguments are of different types and I think that this could be the only solution... sigh.
2

If a,b,c are completely different types, and always will be, then perhaps something like this:

def initialize(*args)
   args.each do |arg| 
      do_a if arg.is_a?(first_type)
      do_b if arg.is_a?(second_type)
      do_c if arg.is_a?(third_type)
   end
end

I'll admit, it is not pretty but it should work. If you are only wanting 3 params then limit the each to only run 3.times.

Comments

0

This could be a solution, but well it's ugly!

def initialize(*args)
    case (args.length)
        when 1
            super(XXX)
            @y = args[0]
        when 2
            if args[0].kind_of?(A) then
                super(args[0])
                @x = args[0]
                @y = args[1]
            elsif args[0].kind_of?(B) then
                super(XXX)
                @y = args[0]
                @z = args[1]
            end 
        when 3
            super(args[0])
            @x = args[0] 
            @y = args[1]
            @z = args[2]
    end
end

Comments

Your Answer

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