Last active
August 29, 2015 14:07
-
-
Save amukiza/e4ce384f45aecbafa74f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
In our first session we said that a variable acts as a named storage location. Or its a name refers to some value | |
in your program. | |
An example would be: | |
=end | |
age = 20 | |
=begin | |
Here age is the name that is refering to the value 20. This means we can use 'age' wherever we would have wanted to use | |
the number 20 but we want to give it a name so that if some one reads our code they will know that we are working with | |
some ones age. Also, It will be easier for us to think about what our program is doing if we are reading names of values. | |
=end | |
next_years_age = age + 1 # we can use age in arithmetic expressions | |
last_years_age = age - 1 # last year this person was 19 e.t.c | |
=begin | |
What if I had four students, I want to represent their ages. | |
would I say, age1 = 20, age2 = 18, age3 = 17 .e.t.c? | |
That could work, but then I have to do something with the ages, I have to get each of them independently, many | |
times we want related data to be represented by the same name. e.g. If I could say: | |
=end | |
ages = [20, 18, 17, 19, 20] #This is called an array. A collection/sequence of related values. | |
=begin | |
We can say that an array is like a variable, only that it keeps more that one value. I have ages which keeps five ages | |
instead of one age. | |
1. What can we do with arrays? | |
Everything we can do with variables and more. | |
=end | |
# Lets see who is the oldest student in our array. | |
puts ages.max #This will return the biggest number in the array which will be 20 | |
# Lets see who is the youngest student in our array. | |
puts ages.min # This return the least/minimum number in the array | |
# Another student has joined, Lets add their age to the array of ages | |
ages << 16 | |
=begin | |
The `<< ` Is refered to as the shovel operator. So we are perfoming a shovel operation :-) | |
"ages.push 16 " is equivalent to the above. But the above is the most prefered way of adding things to an array. | |
=end | |
#Lets find the average of the ages | |
total_ages = ages.inject { |accumulator=0, age| accumulator+= age} | |
number_of_students = ages.size | |
average = total_ages / number_of_students # Our average will be 18. | |
=begin | |
Here we use Inject, Inject collects or folds an array into one value. Using the block or { }. We call it with a { } | |
which takes and accumulator and a gets a value from the array (x) and adds up the x's and keeps them in the | |
accumulator and returns the accumulator | |
We could aslo do it this way. On the same line. | |
ages.inject { |total=0, age| total+= age} / ages.size | |
We shall learn more about { } which is also called a block in ruby during the next session | |
=end | |
# What if we wanted to get the first age | |
puts ages.first | |
# How about the last age | |
puts ages.last | |
# Lets get the first two ages | |
puts ages.take 2 | |
# Lets get the last two ages | |
puts ages.drop 3 | |
# Lets get all ages that are equal to 20 | |
ages.select { |x| x == 20 } # [20, 20] | |
#we have expelled the student whose age is 18. i.e the second age in the array ages. (remember ages = [20, 18, 17, 19, 20]) | |
# Lets remove their age | |
ages.delete 18 # => 18 | |
#or | |
ages.delete_at 1 # => 18 | |
#This will get rid of the students age in the array. | |
# Did I say that we can add arrays ?. Yes we can add arrays and also compare them. | |
even_numbers = [2,4,6,8] | |
odd_numbers = [3,5,7,9] | |
#We can add the two arrays like this. | |
all_numbers = even_numbers + odd_numbers | |
# This will give us all the numbers put together. But not sorted :-( | |
# How do I sort them so that the numbers are in their natural order? | |
all_numbers.sort # => [2, 3, 4, 5, 6, 7, 8, 9] | |
=begin | |
Arrays are not just for numbers | |
------------------------------- | |
Arrays in Ruby allow all types of data. We can easily mix numbers, booleans and strings together. | |
=end | |
mixed_array = ["Andrew", 31, true] | |
# They can also contain other arrays | |
array_with_arrays = [["age", 19], ["age", 20]] | |
# You can mix any thing/type that ruby knows and nest them abitrarily. | |
=begin | |
Harshes | |
-------------------------------- | |
But we don't know whose age that is. we want to keep the student with their age and name | |
=end | |
student = { name: "Alicia", age: 20 } | |
=begin | |
This is is a called a harsh in ruby. A harsh is a key value pair. In our example, the key is name: and the value is "Alicia". | |
age: is also a key to 20. | |
They are called keys, because we use them to access the values. | |
Lets get 20 (I mean, Alicia's age) | |
=end | |
puts student[:age] #20 => nil | |
#No, lets print both age and name | |
puts "#{student[:name]} is #{student[:age]} years old" # Alicia is 20 years old => nil | |
# By the way we can get all the values out of student harsh if we wanted | |
puts student.values # => ["Alicia", 20] | |
# And we can get the keys too :-) | |
puts student.keys # => [:name, :age] | |
#Ohh, alicia wants to change her name? Okay Lets help her. | |
student[:name] = "Kelis" #=> {:name=>"kelis", :age=>20} | |
# And its a new year so her age has chaged too | |
student[:age] = 21 #=> {:name=>"kelis", :age=>201 | |
=begin | |
Enough, with Alicia we actually have many students. why are we favouring one student :-( | |
Let us remember the rest of the students. We can now use our arrays again, Yay!!. Remember we said arrays can keep anything? | |
Now we can keep harshes (students) in the arrays. | |
=end | |
students = [{ name: "Kelly", age: 20 }, | |
{name: "Alex", age: 18 }, | |
{ name: "Kelis", age: 19 }, | |
{ name: "Chris", age: 17 }, | |
{ name: "Raymond", age: 20 } | |
] | |
# Ah, Now we have all our students, Lets the get the first student (Kell) | |
students.first # or student[0] => {:name=>"Kelly", :age=>20} | |
=begin | |
All the stuff we did with ages array we can now do on students. Because Its another array. | |
Only this time it has harshes instead of just numbers. e.g | |
=end | |
students.take 2 # => [{:name=>"Kelly", :age=>20}, {:name=>"Alex", :age=>18}] | |
students.drop 2 # => [{:name=>"Kelis", :age=>19}, {:name=>"Chris", :age=>17}, {:name=>"Raymond", :age=>20}] | |
students.select { |student| student[:age] == 20} #= > [{:name=>"Kelly", :age=>20}, {:name=>"Raymond", :age=>20}] | |
# E.t.c | |
# Finally we can get all their ages out if we wanted to compute average. | |
students.map { |student| student[:age] } # => [20, 18, 19, 17, 20] | |
# Now we can do what we did when we had ages array ages = [20, 18, 19, 17, 20]. | |
#On the same line we can still do this | |
students.map { |student| student[:age] }.inject {|acc=0, age| acc+= age } / students.size | |
# This is gives is the 18 we had when we used ages | |
=begin | |
Sets | |
-------------- | |
A set is a collection where the order does not matter, and the elements are unique Its very simmilar to an array except that | |
its elements are unique. | |
To use sets, we need to include it in our environment. | |
require 'set' | |
Now we can create a new set | |
=end | |
a_set = Set.new [2,3,4,5,5,5] # => #<Set: {2, 3, 4, 5}> | |
another_set = Set.new [2,3,4,5,5,5] # => #<Set: {2, 3, 4, 5}> | |
=begin | |
You will notice that the duplicates were eliminated :-( . All elements have to be unique. | |
We can compare sets, merge them, add and subtract them to each other. | |
a_set & another_set => intersection of the two sets | |
a_set ^ another_set => the difference between the sets of the two sets. Simmillar to (-) | |
a_set | another_set => the union of both sets. Simiilar + (adding the two sets) | |
=end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment