# Method arguments

**URL:** <https://www.ruby-forum.com/t/method-arguments/102079>\
**Category:** Ruby\
**Created:** [August 10, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/method-arguments/102079 "2006-08-10T13:19:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 10, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/1 "2006-08-10T13:19:13Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 10, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/2 "2006-08-10T14:52:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 10, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/3 "2006-08-10T20:33:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 10, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/4 "2006-08-10T21:16:29Z")

</div>

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 🙂

> Thanks.

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/5 "2006-08-15T14:12:46Z")

</div>

On 10/08/06, Sard A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/6 "2006-08-15T14:12:44Z")

</div>

On 10/08/06, Sard A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/7 "2006-08-15T14:22:17Z")

</div>

But one general method of avoiding assignment or restatement is  
instance\_eval

“this is a test”.instance\_eval {self[4…self.length]}

ben

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 15, 2006, 2:14pm UTC](https://www.ruby-forum.com/t/method-arguments/102079/8 "2006-08-15T14:14:47Z")

</div>

On 8/10/06, Sard A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).

Michael G.
