I'll offer provisional support since only American English spells the word "initialize," but think that it should only warn about #initialise if and only if there is no #initialize defined in the class. Something like:
class Class
def method_added(name)
unless self.instance_methods.include?("initialize")
if name.to_s[/\Ainitiali[sz]e\Z/] and name != :initialize
warn "warning: probably misspelled `initialize' as `#{name}'"
end
end
end
end
-austin
I agree with that it should only warn if the current Class does not already have an initialize method by itself. I disagree with that it should only warn about 'initialise' -- mistyping it in other ways is just way too common to ignore it. So I'd suggest this new implementation:
class Class
def method_added(name)
if not private_instance_methods(false).include?("initialize") and
name.to_s[/\Ain[^_]{1,6}li[sz]e\Z/] and name != :initialize
then
warn "warning: probably misspelled `initialize' as `#{name}'"
end
end
end
--Florian Gross
Sigh, this problem would be practically mute if we simply used #init instead.
I agree, but I don't see the name getting changed soon, unfortunately.
-- Florian Gross
I often have a problem when I misspell initialize as "intialize". Tracking down the source of the problem isn't always straightforward. A warning would definitely help.
-- Paul Brannan
How about testing whether there is a method with a similar sounding name as "initialize" defined. Something like levenstein-measure or whatever else (approximate matching). To limit the effect, that should only be applied for methods that start with "init".
I got this several times wrong, mostly "initalize" IIRC.
That would work for me as well -- I only used the Regexp approach because Ruby comes with that built-in -- if Levenstein or Soundex measure is easier to implement and complains about the common typos without causing trouble in other cases then I'm definitely okay with that.
-- Florian Gross
I just don't feel that this belongs in Ruby. Any decent unit testing would quickly reveal this error.
-- Dan
Would it only reveal it or trace it back to the misnaming as well?
-- Florian Gross
Alternative solution, arbitrary initializer names:
class Module
def init(initializer_name)
alias_method(:initialize, initializer_name.to_sym)
end
end
class A
def initialise
...
end
init :initialise
end
class B
def make
...
end
init :make
end
class C
def initialize
...
end
init :initialize
end
If you misspell it again when you call init it raises an exception much earlier. If you don't misspell it again it works fine anyway.
This is a task much better suited to an IDE than an interpreter, just as typos should be caught by web browsers, not DNS servers.
I strongly oppose this suggestion.
- Maybe it's just me but I've never had any problem spelling "initialize".
- I think this has no place in the interpreter, like the previous comment says it's more the job of an IDE/editor.
- This could potentially be very annoying, trying to define a method with a similar name.
As for changing the method name to "init", I find that less rubyistic. I think "init" is more cryptic and less readable.
-austin
I agree with that it should only warn if the current Class does not already have an initialize method by itself. I disagree with that it should only warn about 'initialise' -- mistyping it in other ways is just way too common to ignore it. So I'd suggest this new implementation:
--Florian Gross
Sigh, this problem would be practically mute if we simply used #init instead.
I agree, but I don't see the name getting changed soon, unfortunately.
-- Florian Gross
I often have a problem when I misspell initialize as "intialize". Tracking down the source of the problem isn't always straightforward. A warning would definitely help.
-- Paul Brannan
How about testing whether there is a method with a similar sounding name as "initialize" defined. Something like levenstein-measure or whatever else (approximate matching). To limit the effect, that should only be applied for methods that start with "init".
I got this several times wrong, mostly "initalize" IIRC.
That would work for me as well -- I only used the Regexp approach because Ruby comes with that built-in -- if Levenstein or Soundex measure is easier to implement and complains about the common typos without causing trouble in other cases then I'm definitely okay with that.
-- Florian Gross
I just don't feel that this belongs in Ruby. Any decent unit testing would quickly reveal this error.
-- Dan
Would it only reveal it or trace it back to the misnaming as well?
-- Florian Gross
Alternative solution, arbitrary initializer names:
If you misspell it again when you call init it raises an exception much earlier. If you don't misspell it again it works fine anyway.
This is a task much better suited to an IDE than an interpreter, just as typos should be caught by web browsers, not DNS servers.
I strongly oppose this suggestion.
As for changing the method name to "init", I find that less rubyistic. I think "init" is more cryptic and less readable.