A really cool thing about Ruby is that you can always convert between different types using Ruby's "to" methods.
- to_s converts values to strings.
- to_i converts values to integers (numbers.)
- to_a converts values to arrays.
ARRAYS
Arrays are basically list of stored information.
Create empty array
[]
[517, 66, 07].max => 517
Assign array to a variable
nric=>[517, 66, 07]
Sort an Array
nric.sort =>[07,66,517]
nric=>[517, 66, 07]
// ! permanently sort
nric.sort! =>[07,66,517]
nric =>[07,66,517]
Query Array
nric.include? 07 =>true
Comple list of string methods
Hash
Hashes store related information by giving reusable labels to pieces of data.Create Hash aka Dictionary.
books={}
or
ratings=Hash.new(0)
Stuffing an Array
books["Inferno"] = :splendid
books["Kublai Khan"] = :historical
books["Davinci Code"]=:exciting
books => {"Inferno"=>:splendid, "Kublai Khan=>:historical, "Davinci Code"=>:exciting}
Block
Basically, a block is a chuck of ruby code
No comments:
Post a Comment