Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Method arguments.

Reply
Thread Tools

Method arguments.

 
 
Sard Aukary
Guest
Posts: n/a
 
      08-10-2006
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/.

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      08-10-2006
On 10/08/06, 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]
>
> --
> 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

 
Reply With Quote
 
 
 
 
Paul Battley
Guest
Posts: n/a
 
      08-10-2006
On 10/08/06, 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 this case, it's unnecessary:

"this is a test"[4..-1]

Paul

 
Reply With Quote
 
Ben Nagy
Guest
Posts: n/a
 
      08-10-2006
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]
> Sent: Thursday, August 10, 2006 6:27 PM
> To: ruby-talk ML
> Subject: Re: Method arguments.
>
> On 10/08/06, 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 this case, it's unnecessary:
>
> "this is a test"[4..-1]
>
> Paul
>



 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      08-10-2006
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/.

 
Reply With Quote
 
Sard Aukary
Guest
Posts: n/a
 
      08-10-2006
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/.

 
Reply With Quote
 
Eero Saynatkari
Guest
Posts: n/a
 
      08-10-2006
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflection£¬the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57