Method arguments.

Discussion in 'Ruby' started by Sard Aukary, Aug 10, 2006.

  1. Sard Aukary

    Sard Aukary Guest

    Is there a way to refer to the arguments passed to a function, so I can
    avoid re-stating the argument inside it like the example below?

    puts "this is a test"[4.."this is a test".length]

    --
    Posted via http://www.ruby-forum.com/.
     
    Sard Aukary, Aug 10, 2006
    #1
    1. Advertising

  2. On 10/08/06, Sard Aukary <> wrote:
    > Is there a way to refer to the arguments passed to a function, so I can
    > avoid re-stating the argument inside it like the example below?
    >
    > puts "this is a test"[4.."this is a test".length]
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Is there a reason you can't put it in a varialbe beforehand?

    string = "this is a test"
    puts string[4..string.length]

    Farrel
     
    Farrel Lifson, Aug 10, 2006
    #2
    1. Advertising

  3. Sard Aukary

    Paul Battley Guest

    On 10/08/06, Sard Aukary <> wrote:
    > Is there a way to refer to the arguments passed to a function, so I can
    > avoid re-stating the argument inside it like the example below?
    >
    > puts "this is a test"[4.."this is a test".length]


    In this case, it's unnecessary:

    "this is a test"[4..-1]

    Paul
     
    Paul Battley, Aug 10, 2006
    #3
  4. Sard Aukary

    Ben Nagy Guest

    But one general method of avoiding assignment or restatement is
    instance_eval

    "this is a test".instance_eval {self[4..self.length]}

    ben

    > -----Original Message-----
    > From: Paul Battley [mailto:p]
    > Sent: Thursday, August 10, 2006 6:27 PM
    > To: ruby-talk ML
    > Subject: Re: Method arguments.
    >
    > On 10/08/06, Sard Aukary <> wrote:
    > > Is there a way to refer to the arguments passed to a

    > function, so I can
    > > avoid re-stating the argument inside it like the example below?
    > >
    > > puts "this is a test"[4.."this is a test".length]

    >
    > In this case, it's unnecessary:
    >
    > "this is a test"[4..-1]
    >
    > Paul
    >
     
    Ben Nagy, Aug 10, 2006
    #4
  5. Sard Aukary

    Jim Weirich Guest

    Sard Aukary wrote:
    > Is there a way to refer to the arguments passed to a function, so I can
    > avoid re-stating the argument inside it like the example below?
    >
    > puts "this is a test"[4.."this is a test".length]


    In your particular case, it can be restated as:

    puts "this is a test"[4..-1]

    Where the -1 refers to the end of the string.

    In general, if you have a long expression you wish to refer to twice,
    you can

    (1) make a local variable:

    s = "this is a test"
    puts s[4..s.length]

    or (2) make a method

    def s
    "this is a test"
    end
    # ...
    puts s[4..s.length]

    I'm not sure how your example relates to function arguments ... but is
    this helpfull?

    -- Jim Weirich

    --
    Posted via http://www.ruby-forum.com/.
     
    Jim Weirich, Aug 10, 2006
    #5
  6. Sard Aukary

    Sard Aukary Guest

    Jim Weirich wrote:
    > In your particular case, it can be restated as:
    >
    > puts "this is a test"[4..-1]
    >
    > Where the -1 refers to the end of the string.
    >
    > I'm not sure how your example relates to function arguments ... but is
    > this helpfull?
    >
    > -- Jim Weirich


    Ah yes, -1 is the most obvious way of to get the end reference.

    I was just wondering if there was some sort of reflective way of getting
    a reference to the "this is a test" string from with the [] method.

    Thanks.

    --
    Posted via http://www.ruby-forum.com/.
     
    Sard Aukary, Aug 10, 2006
    #6
  7. Sard Aukary wrote:
    > Jim Weirich wrote:
    >> In your particular case, it can be restated as:
    >>
    >> puts "this is a test"[4..-1]
    >>
    >> Where the -1 refers to the end of the string.
    >>
    >> I'm not sure how your example relates to function arguments ... but is
    >> this helpfull?
    >>
    >> -- Jim Weirich

    >
    > Ah yes, -1 is the most obvious way of to get the end reference.
    >
    > I was just wondering if there was some sort of reflective way of getting
    > a reference to the "this is a test" string from with the [] method.


    No, unless you create one. You could conceivably do this
    by some extremely evil use of method rerouting, local_variables
    and such nefarities.

    Just using variable is your best option, though :)

    > Thanks.



    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Aug 10, 2006
    #7
    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. Storm
    Replies:
    1
    Views:
    459
    Chris Uppal
    May 1, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    4,984
    Josiah Carlson
    Apr 6, 2004
  3. Neo
    Replies:
    10
    Views:
    674
    sushant
    Jan 20, 2005
  4. tutmann
    Replies:
    4
    Views:
    440
  5. Paul Mucur
    Replies:
    4
    Views:
    189
    Roger Pack
    Dec 15, 2009
Loading...

Share This Page