A little idiom I like

Discussion in 'Ruby' started by Hal Fulton, Sep 2, 2006.

  1. Hal Fulton

    Hal Fulton Guest

    Here's a little something I like. YMMV.

    Hal


    require 'pathname'

    class Pathname
    alias / +
    end

    home = Pathname.new("/home/hal")
    proj = home/"projects"
    foo = home/"projects/foobar"
    Hal Fulton, Sep 2, 2006
    #1
    1. Advertising

  2. Hal Fulton wrote:
    > Here's a little something I like. YMMV.
    >
    > Hal
    >
    >
    > require 'pathname'
    >
    > class Pathname
    > alias / +
    > end
    >
    > home = Pathname.new("/home/hal")
    > proj = home/"projects"
    > foo = home/"projects/foobar"
    >
    >

    Already in pathname2. :)

    - Dan
    Daniel Berger, Sep 2, 2006
    #2
    1. Advertising

  3. Hal Fulton

    Phrogz Guest

    Hal Fulton wrote:
    > class Pathname
    > alias / +
    > end


    I like this, both because it's element, AND because I'm not sure I've
    ever seen alias before. #alias_method, yes, but not that. I was about
    to reply "I think you mean 'alias_method :/, :+'", and was surprised to
    find that what you wrote worked as advertised.

    So...'alias' isn't a method? It's a reserved keyword that's part of the
    syntax? And it's "parameters" can be raw method or operator names
    instead of strings or symbols? (I'm looking at the section on
    "Aliasing" in Pickaxe II, but it's still not telling me /what/ 'alias'
    is. Or how it differs from #alias_method.)
    Phrogz, Sep 2, 2006
    #3
  4. Phrogz wrote:
    > So...'alias' isn't a method? It's a reserved keyword that's part of the
    > syntax? And it's "parameters" can be raw method or operator names
    > instead of strings or symbols? (I'm looking at the section on
    > "Aliasing" in Pickaxe II, but it's still not telling me /what/ 'alias'
    > is. Or how it differs from #alias_method.)

    I'm guessing you aren't aware of the "undef" keyword, either.

    HTH,
    Devin
    Devin Mullins, Sep 2, 2006
    #4
  5. Hal Fulton wrote:
    > Here's a little something I like. YMMV.
    >
    > Hal
    >
    >
    > require 'pathname'
    >
    > class Pathname
    > alias / +
    > end
    >
    > home = Pathname.new("/home/hal")
    > proj = home/"projects"
    > foo = home/"projects/foobar"


    That's pretty freaking nifty. Thanks Hal.

    --
    Posted via http://www.ruby-forum.com/.
    William Crawford, Sep 2, 2006
    #5
  6. On 9/1/06, Hal Fulton <> wrote:
    > Here's a little something I like. YMMV.
    >
    > Hal
    >
    >
    > require 'pathname'
    >
    > class Pathname
    > alias / +
    > end
    >
    > home = Pathname.new("/home/hal")
    > proj = home/"projects"
    > foo = home/"projects/foobar"
    >
    >


    Those poor Windows guys. <G>

    irb(main):017:0> class Pathname
    irb(main):018:1> alias \ +
    irb(main):019:1* end
    SyntaxError: compile error
    (irb):18: parse error, unexpected $undefined.
    alias \ +
    ^
    from (irb):19
    from :0
    irb(main):020:0>

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Sep 3, 2006
    #6
  7. Rick DeNatale wrote:
    > On 9/1/06, Hal Fulton <> wrote:
    >> Here's a little something I like. YMMV.
    >>
    >> Hal
    >>
    >>
    >> require 'pathname'
    >>
    >> class Pathname
    >> alias / +
    >> end
    >>
    >> home = Pathname.new("/home/hal")
    >> proj = home/"projects"
    >> foo = home/"projects/foobar"
    >>
    >>

    >
    > Those poor Windows guys. <G>
    >
    > irb(main):017:0> class Pathname
    > irb(main):018:1> alias \ +
    > irb(main):019:1* end
    > SyntaxError: compile error
    > (irb):18: parse error, unexpected $undefined.
    > alias \ +
    > ^
    > from (irb):19
    > from :0
    > irb(main):020:0>
    >


    There's a workaround, right? Some combination of other quotes and double
    backslashes? The R people get around it by using forward slashes
    internally and only converting to backslashes when going out to a
    Windows call that needs them. Even so, there was one thing I never did
    figure out how to do in R, and ended up starting R up in the right
    directory at the beginning of the session. :)
    M. Edward (Ed) Borasky, Sep 3, 2006
    #7
  8. On 9/3/06, M. Edward (Ed) Borasky <> wrote:
    > Rick DeNatale wrote:
    > > On 9/1/06, Hal Fulton <> wrote:
    > >> Here's a little something I like. YMMV.
    > >>
    > >> Hal
    > >>
    > >>
    > >> require 'pathname'
    > >>
    > >> class Pathname
    > >> alias / +
    > >> end
    > >>
    > >> home = Pathname.new("/home/hal")
    > >> proj = home/"projects"
    > >> foo = home/"projects/foobar"
    > >>
    > >>

    > >
    > > Those poor Windows guys. <G>
    > >
    > > irb(main):017:0> class Pathname
    > > irb(main):018:1> alias \ +
    > > irb(main):019:1* end
    > > SyntaxError: compile error
    > > (irb):18: parse error, unexpected $undefined.
    > > alias \ +
    > > ^
    > > from (irb):19
    > > from :0
    > > irb(main):020:0>
    > >

    >
    > There's a workaround, right? Some combination of other quotes and double
    > backslashes? The R people get around it by using forward slashes
    > internally and only converting to backslashes when going out to a
    > Windows call that needs them. Even so, there was one thing I never did
    > figure out how to do in R, and ended up starting R up in the right
    > directory at the beginning of the session. :)


    No easily, even if you can figure out how to define a method named \
    or "\\" it's going to be messy sending it.

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/

    IPMS/USA Region 12 Coordinator
    http://ipmsr12.denhaven2.com/

    Visit the Project Mercury Wiki Site
    http://www.mercuryspacecraft.com/
    Rick DeNatale, Sep 4, 2006
    #8
  9. Hal Fulton

    Hal Fulton Guest

    Rick DeNatale wrote:
    > No easily, even if you can figure out how to define a method named \
    > or "\\" it's going to be messy sending it.


    If we're seriously talking about a Windows usage, let me point out
    that it's only on the command line that \ is needed.

    Internally, WIndows paths can use ordinary slashes (in every case
    I'm familiar with).


    Hal
    Hal Fulton, Sep 4, 2006
    #9
  10. On 9/4/06, Hal Fulton <> wrote:
    > Rick DeNatale wrote:
    >> No easily, even if you can figure out how to define a method named \
    >> or "\\" it's going to be messy sending it.

    > If we're seriously talking about a Windows usage, let me point out
    > that it's only on the command line that \ is needed.


    Mostly.

    > Internally, WIndows paths can use ordinary slashes (in every case I'm
    > familiar with).


    Mostly.

    The reality of the matter is that with the APIs that Ruby is currently
    using -- which are problematic for certain cases (Unicode) -- the \ and
    / are interchangeable internally.

    The advanced APIs -- the Unicode ones and the ones that make it possible
    to have very long path names -- requires \.

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
    Austin Ziegler, Sep 6, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThaDoctor
    Replies:
    3
    Views:
    372
    Alan Woodland
    Sep 28, 2007
  2. Replies:
    30
    Views:
    819
    Eduardo O. Padoan
    Feb 5, 2008
  3. Replies:
    2
    Views:
    1,010
    stoodle
    May 5, 2009
  4. Hal Fulton
    Replies:
    10
    Views:
    235
    Martin DeMello
    May 10, 2005
  5. Daniel
    Replies:
    1
    Views:
    202
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page