Ruby/Arrays Hashes Ranges3

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Hashes page 2

As I did for arrays, here are a few very useful methods that you can use:

How to check if a hash is empty:

pizza.empty?

How to check how many keys are inside the hash:

pizza.size

Get all keys inside the hash in the form of an array

pizza.keys

Get all values assigned to keys in the form of an array (not necessarily in the order they've been entered in the hash)

pizza.values

Image:Hash3.gif

Ranges

Ranges are series of numbers.

Here's how you can define a range:

myrange = 1..10

Myrange will now hold all numbers between 1 and 10 including.Myrange effectively turns into an array.

2nd way of defining a range:

myrange2 = 1...10

Myrange2 will hold all numbers between 1 and 10 except the last number : 10.

As I did with the previously discussed arrays and hashes, here are a few useful methods you can use on ranges.

How to find out the lowest number in a certain range:

puts myrange.min

How to find out the highest number in a certain range:

puts myrange.max

How to check if a number is part of a certain range:

myrange.include? (11)

Displaying a range's contents:

puts myrange.to_a
Previous Next
Personal tools