common constructor idioms

S

Scott Thompson

In C++ one often declares a constructor that accepts another instance
of this class. For those familiar with C++ syntax I'm talking about:

class MyClass {
/* other interesting stuff in this class deleted */

/* Copy constructor */
MyClass(const MyClass &objectToCopy);
};

I guess you could do something similar in Ruby like this:

class MyClass
def initialize(x)
if x.class == MyClass then
#some code to duplicate stuff in x
else
#potentially other initialization mechanisms here
end
end
end


Though there are bound to be differing opinions on the subject (and I
have no desire to start a flame war so please be kind) I was curious to
know is a popular idiom in Ruby code or not? More to the point, would
someone who was using an Ruby extension reasonably assume that this
initialization mechanism was included?

Scott
 
Y

Yukihiro Matsumoto

Hi,

In message "common constructor idioms"

|In C++ one often declares a constructor that accepts another instance
|of this class. For those familiar with C++ syntax I'm talking about:
|
|class MyClass {
| /* other interesting stuff in this class deleted */
|
| /* Copy constructor */
| MyClass(const MyClass &objectToCopy);
|};
|
|I guess you could do something similar in Ruby like this:
|
|class MyClass
| def initialize(x)
| if x.class == MyClass then
| #some code to duplicate stuff in x
| else
| #potentially other initialization mechanisms here
| end
| end
|end

How about leaving "initialize" for basic initialization, and define
class methods for each object construction scheme, with proper names?

matz.
 
R

Ryan Dlugosz

Scott Thompson said:
Though there are bound to be differing opinions on the subject (and I
have no desire to start a flame war so please be kind) I was curious to
know is a popular idiom in Ruby code or not? More to the point, would
someone who was using an Ruby extension reasonably assume that this
initialization mechanism was included?

That would work, but I'd suspect that it isn't popular around these parts.
I would also suggest that it's probably *not* safe to assume that type of
initialization system is included with any given extension...

It would be a much clearer implementation to specifically define a
deep_copy method in your class, a la:

def deep_copy
Marshal.load(Marshal.dump(self))
end

Of course, if you don't need a deep copy, you've already got
Object.clone... someone smack me if I'm way off base here. :)

-Ryan
 
D

Dave Thomas

Scott said:
In C++ one often declares a constructor that accepts another instance of
this class. For those familiar with C++ syntax I'm talking about:

class MyClass {
/* other interesting stuff in this class deleted */

/* Copy constructor */
MyClass(const MyClass &objectToCopy);
};

I guess you could do something similar in Ruby like this:

class MyClass
def initialize(x)
if x.class == MyClass then
#some code to duplicate stuff in x
else
#potentially other initialization mechanisms here
end
end
end


Though there are bound to be differing opinions on the subject (and I
have no desire to start a flame war so please be kind) I was curious to
know is a popular idiom in Ruby code or not? More to the point, would
someone who was using an Ruby extension reasonably assume that this
initialization mechanism was included?

Personally I wouldn't overload #new like this. Instead I'd add a new
static construction method

class Boat
def Boat.from_other_boat(b)
# ...
end
end


...or... I'd simply implement an instance method to duplicate a
particular object.

I wrote a blog entry on constructors:

http://pragprog.com/pragdave/Practices/ConstructionMethods.rdoc,v


Cheers


Dave
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top