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 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
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-forum.com/\.
In this case, it's unnecessary:
"this is a test"[4..-1]
Paul
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]
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/\.
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 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/\.
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 WeirichAh 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 Guterl
On 8/10/06, Sard Aukary <sardaukary@yahoo.co.uk> wrote:
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