On Wed, Jul 28, 2010 at 4:47 PM, Travis H.
<[address removed]> wrote:
> On Wed, Jul 28, 2010 at 06:36:42PM -0400, Ian Smith-Heisters wrote:
>> Can you do it in initialize?
> No, that is the canonical answer, but I have the same method signature
> for each case.
> Besides, that's evil. ��If you want to add parameters, change their
> types, or have default arguments, it gets really hairy to make sure
> each invocation works properly.
> OTOH, I think it would be possible to pass a symbol like :from_string
> as the first argument, and have something like:
> def initialize(style, *args)
> ��case style
> ��when :from_string then initialize_from_string(*args)
> ��when :from_filename then initialize_from_filename(*args)
> What do you folks think about that?
I think that violates POLS. The Ruby way is just have the user pass in
whatever and Do The Right Thing. If you really don't want to do that,
def self.build_from_string string
new.string = string
>> Other options: if you're feeling like Evel Knievel, override ::new.
> Not familiar with the ::syntax.
"::" denotes a static method, like # denotes and instance method. I'm
saying override ClassName::new to Do The Right Thing. That should only
be done if you really can't do it in initialize, eg. if you want to
allocate and return a subclass instead of the class ::new was called
> What I'm wanting to do is ignore ClassName.new entirely and simply
> have three class methods that construct and initialize objects.
Then go with the build_ method described above.
>> > 2) Need to do array -= [ x ], and subtract anything in array
>> > ?? that is eql? to x (not x itself, but merely having same data).
>> > ?? Got any ideas how to implement this easily?
>> I wouldn't recommend it. But, if you really want to, and `x` is an
>> instance of a custom class, you need to override (IIRC) #== and #hash.
>> This is because Array#- and Array#uniq use a hash internally.
> I think the alternative is to implement my own set operations (add and
> subtract) using eql? instead of ==, but that seems pretty annoying
> The issue here is that I want to say "remove all the files from this
> array that have been backed up before", where "what has been backed up
> before" is actually serialized to a file on disk, and so won't be the
> identical file metadata object - merely eql? to what I've told the
> program to back up.
Use Array#select or Array#reject. It'll make it much clearer what you're doing.
The only time I would override #hash and #== would be if this is
something that's all over the system. Overriding something like #==
(especially on a standard class) is a broad, systemic change, and
should only be done if you're representing broad, systemic
So, if your app is all about managing files and their backups, then
maybe. Otherwise, KISS.
> PS: Grr... both the ruby-lang.org MLM and meetup.com's web app don't
> think plus signs are valid in the local-part of email addresses. ��Why
> doesn't anyone ever RTF RFC2822?
> A Weapon of Mass Construction
> My emails do not have attachments; it's a digital signature that your mail
> program doesn't understand. | http://www.subspa...
> If you are a spammer, please email [address removed] to get blacklisted.
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> This message was sent by Travis H. ([address removed]) from The East Bay Ruby Meetup Group.
> To learn more about Travis H., visit his/her member profile: http://www.meetup...
> To unsubscribe or to update your mailing list settings, click here: http://www.meetup...
> Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]