Initialise

I

Iain Barnett

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..."=20

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=
 
I

Iain Barnett

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... :)


Note that it doesn=92t make other code (notably,
BasicObject.new) call #initialise all of a sudden, which is probably
what you=92d 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 :)


Regards,
Iain
 
B

Brian Candler

class Object
alias :eek:ld_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 :)

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

Iain Barnett

class Object
alias :eek:ld_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.

Thanks, I'll give that a shot.
That will be released some time between perl 6 and hell freezing over
:)

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

Regards
Iain
 
R

Robert Klemme

2010/8/26 Brian Candler said:
class Object
=A0alias :eek:ld_initialize :initialize
=A0def initialize(*args,&blk)
=A0 =A0if respond_to? :initialise
=A0 =A0 =A0initialise(*args,&blk)
=A0 =A0else
=A0 =A0 =A0old_initialize(*args,&blk)
=A0 =A0end
=A0end
end

class Foo
=A0def initialize(n)
=A0 =A0@n =3D n
=A0end
end

class Bar
=A0def initialise(m)
=A0 =A0@m =3D m
=A0end
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".
That will be released some time between perl 6 and hell freezing over
:)

Can't be too long though. :)

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top