Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby is there a way to overload the initialize constructor?

Tags:

ruby

In Java you can overload constructors:

public Person(String name) {   this.name = name; } public Person(String firstName, String lastName) {    this(firstName + " " + lastName); } 

Is there a way in Ruby to achieve this same result: two constructors that take different arguments?

like image 783
ab217 Avatar asked Oct 18 '10 11:10

ab217


People also ask

What does initialize method do in Ruby?

The initialize method is useful when we want to initialize some class variables at the time of object creation. The initialize method is part of the object-creation process in Ruby and it allows us to set the initial values for an object.

What is constructor in Ruby?

A constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Like methods, a constructor may also contain the group of instructions or a method which will execute at the time of object creation.

How do you stop a constructor from overloading in Java?

Another option is to use a "parameter object" following the builder pattern - create another class whose sole purpose is to hold the data for the constructor parameters. This should be mutable, with setters for all of the different values.


2 Answers

The answer is both Yes and No.

You can achieve the same result as you can in other languages using a variety of mechanisms including:

  • Default values for arguments
  • Variable Argument lists (The splat operator)
  • Defining your argument as a hash

The actual syntax of the language does not allow you to define a method twice, even if the arguments are different.

Considering the three options above these could be implemented with your example as follows

# As written by @Justice class Person   def initialize(name, lastName = nil)     name = name + " " + lastName unless lastName.nil?     @name = name   end end   class Person   def initialize(args)     name = args["name"]     name = name + " " + args["lastName"] unless args["lastName"].nil?     @name = name   end end  class Person   def initialize(*args)     #Process args (An array)   end end 

You will encounter the second mechanism frequently within Ruby code, particularly within Rails as it offers the best of both worlds and allows for some syntactic sugar to produce pretty code, particularly not having to enclose the passed hash within braces.

This wikibooks link provides some more reading

like image 130
Steve Weet Avatar answered Sep 26 '22 03:09

Steve Weet


I tend to do

class Person   def self.new_using_both_names(first_name, last_name)     self.new([first_name, last_name].join(" "))   end    def self.new_using_single_name(single_name)     self.new(single_name)   end    def initialize(name)     @name = name   end end 

But I don't know if this is the best approach.

like image 44
Andrew Grimm Avatar answered Sep 23 '22 03:09

Andrew Grimm