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]

Sard A. 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 W.

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

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.

Sard A. wrote:

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

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 :slight_smile:

Thanks.

On 10/08/06, Sard A. [email protected] 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

On 10/08/06, Sard A. [email protected] 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

But one general method of avoiding assignment or restatement is
instance_eval

“this is a test”.instance_eval {self[4…self.length]}

ben

On 8/10/06, Sard A. [email protected] wrote:

– Jim W.

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.

Are you looking for something like this?

class String
def your_method
self[4…self.length]
end
end

“this is a test”.your_method
=> " is a test"

Thanks.


Posted via http://www.ruby-forum.com/.

Michael G.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs