3

So, because I am British, I always initialise objects with the name initialise() and quite often get the error Error in initialize: (wrong number of arguments (given n, expected 0)).

Is there some meta programming I can do to make initialize() for all objects try to call initialise()?

5
  • 4
    I agree with you on the spelling preference, but it might not be a good idea to write your code with this deviation from what people expect. At least the language it's written in is pretty close to your first language: some people don't even have that advantage. Commented Mar 7, 2019 at 9:36
  • @Sancarn : You open a box of worms here. What if both initialize and initialise are defined in a class? Which one has "preference"? Also, you might use a library or gem where someone legally defines a self.initialise, which however is not intended to be used as constructor, and at the same time has not defined a initialize method, because he is happy with the default. I certainly would not want to debug errors resulting from this kind of problem.... Commented Mar 7, 2019 at 10:22
  • Yes @user1934428 you are quite correct, that is a potential issue. That being said, it's also going to be such a rare occurrence that I'm willing to accept it as a weakness. Could also throw a "WARN" to STDOUT, which would allow you to easily identify where it fails. Commented Mar 7, 2019 at 10:29
  • It is interesting that this a concern for you such that you are treating this as a written paper in your native tongue. What if matz had written ruby in Japanese (his native tongue) and you had to type "初期化する" would you still have the same concern? method definitions are symbolic and only have the meaning that the underlying code defines. Commented Mar 7, 2019 at 15:48
  • @engineersmnky If matx had written Ruby in Japanese, I would not be using Ruby at all, as is the case for many people. Do you use the language Aheui?... The fact of the matter is, its very annoying when you get loads of errors constantly due to Americanizations... I get your point, but would you use Ruby if the method was named asd98fi12jk2naoshujibdh1sdasd2asdfa2rfxs? There's a reason why people don't use lolcode language for serious projects... It's annoying, as is initialize Commented Mar 7, 2019 at 16:09

3 Answers 3

4

Something like this might work; but anyone reading or using your code might hate you. :P

class BasicObject
  alias american_initialize initialize
  private def initialize(*args, &block)
    if respond_to?(:initialise)
      initialise(*args, &block)
    else
      american_initialize(*args, &block)
    end
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm :) I'll add a warning message in there just in case it is "accidentally" called at the wrong time :P
2

Please don't do this. Ever. But let's teach people how to shoot themselves on the foot:

class BasicObject
  def self.new(*args)
    if instance_methods.member? :initialise
      allocate.initialise(*args)
    else
      super
    end
  end
end

So what's happening here: We just overwrote the .new definition. By default it calls #allocate and then #initialize passing down the *args you sent to it.

.allocate allocates the memory for the object but does not call #initialize over it, so we check if the object has a #initialise instance method defined, if it has it gets called, otherwise default behaviour (super) is used.

3 Comments

What is the super of BasicObject.new?
@sawa that should be Class#new in this case (and I think it would be cleaner / less confusing to just override Class#new instead).
@sawa Yep. super will be called on the context of the invoker and will travel up the inheritance chain until it finds .new defined somewhere (usually Class). @Stefan I'm not sure if overriding this in Class would work, or if any changes would need to be made as Class.new is kinda of a special beast.
0

The did_you_mean gem (comes with Ruby 2.3 and later) can help you to avoid such errors:

require 'did_you_mean/experimental'

class Foo
  def initialise
  end
end

Output:

warning: initialise might be misspelled, perhaps you meant initialize?

The code behind this feature uses method_added to monitor the addition of new methods and prints a warning if the Levenshtein distance between the method's name and "initialize" is ≤ 2.

1 Comment

Weird that you have to require the module... Thanks for the work around, although it isn't what I was after, for some this may be ideal. Unfortunately for me it won't really work though given that I am in 1.9.3 and cannot upgrade.

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.