Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2008, 04:23 PM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: USA
Posts: 21 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m 10 sec
Reputation Power: 0
Array assignment problem

Hi guys,

Code:
@theArray = []
@theArray[0]= 155
@theArray[1]= 55

@theArray2 = []

for i in (0..2)
 @theArray2[i] = @theArray[i].to_s(16)
end


doesnt work ?

ArgumentError: wrong number of arguments (1 for 0)
from (irb):75:in `to_s'
from (irb):75
from (irb):74:in `each'
from (irb):74
from ^C:0

I want to be converting the elements of theArray to hex and copy them to the new theArray2. And do this in one step.

Does anyone know how to fix this ?

Reply With Quote
  #2  
Old July 19th, 2008, 06:01 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Posts: 1,284 MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 7 h 28 m 11 sec
Reputation Power: 342
you are using an array with 2 items, placed at index 0 and 1.

in your loop you have (0..2) which will access item at index 0, 1 and 2, with index 2 no existing.

what you want to use is (0...2) (with an extra dot, which means the vale 2 is not being used) or (0..1).

Reply With Quote
  #3  
Old July 19th, 2008, 06:41 PM
m4st3rm1nd m4st3rm1nd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: USA
Posts: 21 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m 10 sec
Reputation Power: 0
thanks for the quick reply.

Reply With Quote
  #4  
Old July 19th, 2008, 09:57 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 649 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 53 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
To avoid such worries, ruby moves pretty heavily to the iterator philosophy. Arrays support iterators like each
Code:
irb(main):005:0> a = [1,4,2,7,6]
=> [1, 4, 2, 7, 6]
irb(main):006:0> a.each { |x| puts x }
1
4
2
7
6
=> [1, 4, 2, 7, 6]

Now you dont need to worry about whether or not to add the extra '.' or not.
Just food for thought.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Array assignment problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT