Initialise

Hi all,

Being English and able to spell proper like, innit, I find I’m creating
a lot of stupid errors by writing def initialise a lot. I had the
bright idea of making an alias to stop this transatlantic turmoil, but
I’ve no idea where to put it. Everything I’ve looked up boils down to
“don’t worry, initialize is sort of magic behind the scenes, just call
new…”

I tried this:

class Object
alias :initialise :initialize
end

but it didn’t work. Is this possible, or do I have to start writing like
the lead singer from Radiohead sings?

Regards,
Iain

Iain B.:

Being English and able to spell proper like, innit, I find I’m
creating a lot of stupid errors by writing def initialise
a lot.

:slight_smile:

I tried this:

class Object
alias :initialise :initialize
end

but it didn’t work.

It definitely does work – you can start using #initialise instead of
#initialize in your code. Note that it doesn’t make other code (notably,
BasicObject.new) call #initialise all of a sudden, which is probably
what you’d rather have, innit?

(You probably want to redeclare BasicObject.new,
except you probably don’t want to, not really.)

— Piotr S.

I see your point.

I would follow the language. Issues with other coders and such will
arise…

Alias for Initialize doesn’t fit.

  1. Initialize is a private method.
  2. The Object inherently calls Initialize upon creation. When the object
    is
    instantiated its calls Initialize. Alias is created by this time, and
    there
    is no no need to call it after creation.
  3. Aliasing in the way you are attempting is calling it a second time.

Its probably better to follow the language.

Just a quick irb session:

irb(main):001:0> class Joe
irb(main):002:1> alias :initialise :initialize
irb(main):003:1> def initialize
irb(main):004:2> print “where you going with that gun in your hand?”
irb(main):005:2> @gun = 0
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> jimi = Joe.new
where you going with that gun in your hand?=> #<Joe:0x36a22c @gun=0>
irb(main):009:0> jimi.initialise
NoMethodError: private method `initialise’ called for #<Joe:0x36a22c
@gun=0>
from (irb):9

On 26 Aug 2010, at 15:03, Joseph E. Savard wrote:

I would follow the language. Issues with other coders and such will arise…

The language I was trying to follow was English, but there you go… :slight_smile:

On 26 Aug 2010, at 14:49, Piotr S. wrote:

Note that it doesn’t make other code (notably,
BasicObject.new) call #initialise all of a sudden, which is probably
what you’d rather have, innit?

That was the one I was going for. Ah well.

Thanks both for your input. I suppose my only recourse now is to start
hacking on the Ruby source and get my patch submitted in time for Ruby
2.0 :slight_smile:

Regards,
Iain

class Object
alias :old_initialize :initialize
def initialize(*args,&blk)
if respond_to? :initialise
initialise(*args,&blk)
else
old_initialize(*args,&blk)
end
end
end

class Foo
def initialize(n)
@n = n
end
end

class Bar
def initialise(m)
@m = m
end
end

p Foo.new(123)
p Bar.new(456)

You get a warning about redefining initialize, but it seems to work.

I suppose my only recourse now is to start
hacking on the Ruby source and get my patch submitted in time for Ruby
2.0 :slight_smile:

That will be released some time between perl 6 and hell freezing over
:slight_smile:

On 26 Aug 2010, at 15:54, Brian C. wrote:

end

p Foo.new(123)
p Bar.new(456)

You get a warning about redefining initialize, but it seems to work.

Thanks, I’ll give that a shot.

I suppose my only recourse now is to start
hacking on the Ruby source and get my patch submitted in time for Ruby
2.0 :slight_smile:

That will be released some time between perl 6 and hell freezing over
:slight_smile:

I was thinking of using Perl6 to write Ruby2.0 :slight_smile:

Regards
Iain

2010/8/26 Brian C. [email protected]:

end

p Foo.new(123)
p Bar.new(456)

You get a warning about redefining initialize, but it seems to work.

What about inheritance especially in light of “super”? I’m with
Joseph here: better adjust to the programming language and stick with
“initialize”.

I suppose my only recourse now is to start
hacking on the Ruby source and get my patch submitted in time for Ruby
2.0 :slight_smile:

That will be released some time between perl 6 and hell freezing over
:slight_smile:

Can’t be too long though. :slight_smile:

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs