[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Method arguments.

Sard Aukary

8/10/2006 11:19:00 AM

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-....

6 Answers

Farrel Lifson

8/10/2006 11:27:00 AM

0

On 10/08/06, Sard Aukary <sardaukary@yahoo.co.uk> 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-....
>
>
Is there a reason you can't put it in a varialbe beforehand?

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

Farrel

Paul Battley

8/10/2006 11:27:00 AM

0

On 10/08/06, Sard Aukary <sardaukary@yahoo.co.uk> 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

8/10/2006 11:53:00 AM

0

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:pbattley@gmail.com]
> Sent: Thursday, August 10, 2006 6:27 PM
> To: ruby-talk ML
> Subject: Re: Method arguments.
>
> On 10/08/06, Sard Aukary <sardaukary@yahoo.co.uk> 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
>


Jim Weirich

8/10/2006 12:52:00 PM

0

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-....

Sard Aukary

8/10/2006 6:33:00 PM

0

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-....

e

8/10/2006 7:17:00 PM

0

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-....