align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

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

From: user 5.
Sent on: Wednesday, July 28, 2010 3:36 PM
On Wed, Jul 28, 2010 at 3:18 PM, Travis H.
<[address removed]> wrote:
> 1) Need multiple ways to construct objects, with arguments (or
> �� signature) of the same type.
> �� What's canonical way? ��Create a class method similar to new()
> �� that calls a different method than initialize?
> �� Right now I do something like:
> �� a =­_from_string(str)
> �� b =­_from_filename(fn)
> �� In this case, initialize is simply empty or non-existent.
> �� Seems like a waste of keystrokes to have new() in there.

Can you do it in initialize?

class ClassName
  def initialize pathname_or_string
    case pathname_or_string
    when Pathname then initialize_with_path­name pathname_or_string
    when String then initialize_with_stri­ng pathname_or_string
    else; raise Argument, "invalid pathname or string:

Other options: if you're feeling like Evel Knievel, override ::new.
Or, create static methods that massage your argument and pass them to
::new() (ClassName.new_from_­pathname)
> 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.

> 3) Is it really true that there's no way to call an instance method in
> �� a parent class except from the same-named method in the subclass?
> �� super doesn't return a real object, so I'm just curious if there's
> �� a workaround other than renaming methods.

Huh. In many years of doing Ruby, I've never once needed to do that. I
don't think there *is* a way of doing that. I say it's a code smell.
Find some other way. One way would be to, instead of overriding the
parent method in your subclass, alias it so it's still accessible
under a different name.

> 4) Does Class#method mean an instance method, or a class method?
> �� I get confused and never ever saw this explained anywhere.

it's an instance method. I forget where this comes from, but it
confused me for years too.

> For anyone who is interested in the project, it's here:
> http://www.subspa...­
> I'm also slowly going back through my SVN logs and writing up a
> "evolution of the design of HDB" document. ��As a programmer of some 30
> years, I have never come across a document describing the actual
> process of programming. I recall hearing once that looking at a
> finished mathematical proof leads you to believe the mathematician was
> a genius, and also gives no clue as to how to actually create a
> proof. It is therefore most instructive to look at how they actually
> develop their proofs so that one gets a feel for the process (and
> realize that it is not as intimidating as it seems, but does require a
> lot of hard work, just like programming). ��Here's what I have so far:
> http://www.subspa...­
> It should be interesting to people learning OOD/OOP and/or ruby.
> --
> 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