Method arguments.

S

Sard Aukary

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]
 
F

Farrel Lifson

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]
Is there a reason you can't put it in a varialbe beforehand?

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

Farrel
 
P

Paul Battley

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
 
B

Ben Nagy

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[email protected]]
Sent: Thursday, August 10, 2006 6:27 PM
To: ruby-talk ML
Subject: Re: Method arguments.

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
 
J

Jim Weirich

Sard said:
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
 
S

Sard Aukary

Jim said:
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.
 
E

Eero Saynatkari

Sard said:
Jim said:
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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top