26284 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Soylent Corporation
prepares to DEFEAT
FOOD
VMware public cloud
aims at ESXi
customers, not AWS
Microsoft reveals
Xbox One, the
console that can
read your heartbeat
New Intel CEO
Krzanich takes
reins of core
product groups
CSIRO scales up
solar cell printing
Cook: Apple has "no
current plan" to
pull profits out of
Ireland
AWS cloud gains
critical federal
security
certification
IBM puts
supercomputer
Watson to work in
ROBOT CALL CENTRE
Irish deputy PM:
You want more tax
from Apple? Your
problem, not ours
Private equity firm
coughs £1bn for
Websense
Slashdot
EPA Makes a Rad
Decision
Ask Slashdot: Can
Yahoo Actually
Stage a Comeback?
3-D Printable Food
Gets Funding From
NASA
Transporting a
15-Meter-Wide,
600-Ton Magnet
Cross Country
House Bill Would
Mandate Smart Gun
Tech By U.S.
Manufacturers
Do Developers Need
Free Perks To
Thrive?
So You"ve Always
Wanted a
Hovercraft...
(Video)
Microsoft Unveils
Xbox One
Ask Neil Gaiman and
Amber Benson About
Their Kickstarter
Vampire Movie
Immigration Reform
May Spur Software
Robotics
Article viewer

Arrays and Hashes



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:Ruby
Search OSI about Ruby.More articles by dimport.
 viewed 6248 times send this article printer friendly

Digg this!
    Rate this article :
In keeping with the general theme of ruby, its array/hash handling (and infact, data structures in general) is simple, easy to use, whilst being powerful enough to get the job done.

Lets start by creating a simple array

an_array = [“osi”, 15, 31337]

Woah there fucko, different types in the same array ? (second nature to perl coders, still a novelty to me) Thats right kids, although the dreaded offset still applies;

puts an_array[1]
Produces the output “15”

an_array[1] = 25
reassigns a value in the array;

an_array[6] = “t00t”

“hold on, i thought the array was only 3 objects big ?” well now its 6 objects big, ruby arrays grow as needed, ah the joys.

puts an_array

reveals all, it fills in any unspecified values with ‘nil’
A crude script that shows the basics;

an_array = ["osi", 15, 31337]
puts an_array

an_array[1] = 25
puts an_array[1]

an_array[6] = "t00t"
puts an_array

an_array[4,0] = ["inserted", "strings"]
puts an_array

an_array[0]=%w(blah, blah, blah)
puts an_array

A few of the more useful methods you’d use to handle arrays;

an_array = [109, 15, 103, 153, 94]
puts an_array.min #or max
Returns the min/max value.

an_array = [109, 15, 103, 153, 94]
puts an_array.sort

an_array = [109, 15, 103, 153, 94]
an_array[12] = 5

puts an_array
puts an_array.compact

Deletes all elements with the value ‘nil’
an_array = [109, 15, 103, 153, 94, 94]

puts an_array.uniq
Returns only unique elements of an array.

an_array = %w(ab, bc, cd, de, ef,)
puts an_array.grep(/e/)

Just like grep in TEH LUNIX, here it returns de and ef. Hashes are just as simple;

 days_of_the_week = {
“First” => “Monday”,
“Second” => “Tuesday”,
“Third” => “Wedensday”,
“Fourth” => “Thursday”,
“Fifth” => “Friday”,
“Sixth” => “Saturday”,
“Seventh” => “Sunday”
}


puts days_of_the_week.keys or puts days_of_the_week.values
Returns either the keys or values of a hash.

puts days_of_the_week.key?(“second”) #true
puts days_of_the_week.values?(“march”) #false

Checks if the specified term exists as a key or value.
puts days_of_the_week.key.to_a

Returns a copy of the hash in array form.

This article was originally written by pigsbig78

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
reacher
The Ruby Way on Wed 27th Aug 6pm
I've recently come back to OSIX and taken all the challenges again. This next time around, however, I'm using Ruby to tackle them. I will admit that my old C versions of some solutions ran faster (especially the brute force ones), but coding the Ruby ve
bb
Ruby on Rails on Tue 10th Oct 9am
I installed ruby and wrote my first ruby on rails web application. It was quite an enjoyable experience. Certainly more fun than asp.net (which is bloody unproductive) I suggested we knock some internal application up in it for my company to have ap


     
Your Ad Here
 
Copyright Open Source Institute, 2006