addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupsimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1outlookpersonJoin Group on CardStartprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

Re: [ruby-81] a few simple ruby (non-rails) questions

From: user 5.
Sent on: Wednesday, July 28, 2010 5:00 PM
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_stri­ng(*args)
> ��when :from_filename then initialize_from_file­name(*args)
> end
> 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,

class ClassName
  def self.build_from_stri­ng 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 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
> too.
> 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 MLM and'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])
> http://www.meetup...­
> 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]

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy