Hello people, Lets say i have arr = [100] and i = 100 Now i do calculation: i * arr[0] ==> 10000 # works ok but i * arr ==> Error Why arr != arr[0] with only one element in the array? And is there a way to 'free' the first and only object in arr? As if it never belonged to an array, like 'i' ? I know with a float or integer one can use: a.to_s.to_i or a.to_s.to_f but how about other objects? Thanx for your thoughts!
on 2013-02-11 13:26
on 2013-02-11 14:04
Am 11.02.2013 13:27 schrieb "Ronnie Aa" <lists@ruby-forum.com>: > but > i * arr ==> Error > > Why arr != arr[0] with only one element in the array? arr is the whole Array object and arr[0] is the first element of the array (= 100) and these are not equal.
on 2013-02-11 14:43
Thomas Preymesser wrote in post #1096273: > Am 11.02.2013 13:27 schrieb "Ronnie Aa" <lists@ruby-forum.com>: >> but >> i * arr ==> Error >> >> Why arr != arr[0] with only one element in the array? > > arr is the whole Array object and arr[0] is the first element of the > array > (= 100) and these are not equal. Yes but when the array contains only ONE element there's no real need to indicate the position of that element. So i want to use arr as if it was arr[0]. But that's no possible.. thnx for your answer
on 2013-02-11 14:51
Subject: Re: firtst and only element of array (noob level) Date: lun 11 feb 13 10:43:27 +0900 Quoting Ronnie Aa (lists@ruby-forum.com): > Yes but when the array contains only ONE element > there's no real need to indicate the position of that element. > So i want to use arr as if it was arr[0]. But that's no possible.. You have to think of the array as the chest of apples. When you have only one apple left, the chest does not disappear. If it did, the fact would generate a lot of confusion... Carlo
on 2013-02-11 15:13
Carlo E. Prelz wrote in post #1096281: > Subject: Re: firtst and only element of array (noob level) > Date: lun 11 feb 13 10:43:27 +0900 > > Quoting Ronnie Aa (lists@ruby-forum.com): > >> Yes but when the array contains only ONE element >> there's no real need to indicate the position of that element. >> So i want to use arr as if it was arr[0]. But that's no possible.. > > You have to think of the array as the chest of apples. When you have > only one apple left, the chest does not disappear. If it did, the > fact would generate a lot of confusion... > > Carlo Ok I see thnx
on 2013-02-11 17:04
You could always run a check of the array and see how many items are in it, if there is only one, then do something like: array.first that way you don't have to use [0]. Wayne ----- Original Message ---- From: Ronnie Aa <lists@ruby-forum.com> To: ruby-talk ML <ruby-talk@ruby-lang.org> Sent: Mon, February 11, 2013 8:14:24 AM Subject: Re: firtst and only element of array (noob level) Carlo E. Prelz wrote in post #1096281: > only one apple left, the chest does not disappear. If it did, the > fact would generate a lot of confusion... > > Carlo Ok I see thnx
on 2013-02-11 17:57
Wayne Brisette wrote in post #1096317: > You could always run a check of the array and see how many items are in > it, if > there is only one, then do something like: > > array.first > > that way you don't have to use [0]. > > Wayne > Hi Wayne, Thnx for your suggestion
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.