You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

97 lines
5.0 KiB

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Learning Website</title>
<link href="/styles/styles.css" rel="stylesheet" type="text/css">
<link href="/programming/styles/styles.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="banner">
<h1 class="courselink">Ruby</h1>
<h2 class="lecturer">Personal Notes : Philip Osztromok</h2>
<h2 class="episodetitle">Part Two</h2>
</div>
<article>
<h2 class="sectiontitle">Introduction to hashes</h2>
<p>We have seen that an array is a collection of items which are indexed from 0 up to the length of the array minus 1. To illustrate this, if an array (which we will call simply array[]) has 4 elements in it. The index of the first element in the array is given as array[0] and the index of the fourth (and last) element is given as array[3].</p>
<p>If the array had n items the index of the first element would still be array[0] and the index of the last (or nth) element would be given as n-1.</p>
<p>In some situations, we might want to contrive a situation where the elements are indexed from 1 rather than 0 or we may indeed wish to use a non-numeric index and we can do this with hashes.</p>
<p>Essentially, a hash is a collection of key-value pairs and the syntax is:</p>
<pre class="inset">
hash = {
key1 =&gt; value1,
key2 =&gt; value2,
key3 =&gt; value3
}</pre>
<p>An example of this might look something like:</p>
<pre class="inset">
my_hash = { "name" =&gt; "Eric",
"age" =&gt; 26,
"hungry?" =&gt; true
}
puts my_hash["name"]
puts my_hash["age"]
puts my_hash["hungry?"]</pre>
<p>The output generated by this code is</p>
<pre class="inset">
Eric
26
true</pre>
<p>Note that when we are accessing parts of the hash, we are using syntax which is similar to that which we would use to access an element of an array but in this case, rather than providing an index value for the element we want to access, we are using the key and the corresponding value is returned (and in this case sent to the console as output).</p>
<p>We can also create a new empty hash with a statement such as:</p>
<p class="inset">pets = Hash.new</p>
<p>Note that Ruby keywords are case-sensitive and so it is important to remember that Hash must be capitalized.</p>
<p>We can add an element to a hash by providing the hash name, the key and the value as follows:</p>
<p class="inset">pets[“Stevie”] = “cat”</p>
<p>Note that in this example, Stevie is the key and cat is the value.</p>
<h2 class="sectiontitle">Iterating through an array</h2>
<p>We saw previously how we could use each (see under arrays) to iterate through an array. Ruby also provides a shorthand for this.</p>
<pre class="inset">
numbers[1,2,3,4,5]
numbers.each {|element| puts element}</pre>
<p>Note that element here is not a keyword, this is merely a formal parameter so we could use any word here such as number provided we also use the word number in the command to be executed.</p>
<p>The formal parameter here effectively becomes a reference to each element of the array in successive iterations and that allows us to execute some code using this reference without having to worry about providing an index to make sure we are referencing the correct element in each successive iteration.</p>
<p>The code here has only one statement to be executed in each iteration but it looks as though it might be possible to put more than one statement in the code block.</p>
<p>In fact, this is possible but it changes the syntax somewhat. Rather than using curly braces we use the commands do and end with do going before the formal parameter and this will give us something like:</p>
<pre class="inset">
languages = ["HTML", "COBOL", "Java", "Ruby", "PHP", "Basic", "C"]
languages.each do |language|
puts language
puts
end</pre>
<p>Note that this syntax didn’t work in the CodeAcademy window but works fine when run through the Ruby interpreter.</p>
<img src="images/image1.png" alt="image1">
</article>
<div class="btngroup">
<button class="button" onclick="window.location.href='partone.html';">
Previous Chapter - Part One
</button>
<button class="button" onclick="window.location.href='partthree.html';">
Next Chapter - Part Three
</button>
<button class="button" onclick="window.location.href='ruby.html'">
Course Contents
</button>
<button class="button" onclick="window.location.href='/programming/programming.html'">
Programming Page
</button>
<button class="button" onclick="window.location.href='/index.html'">
Home
</button>
</div>
</body>
</html>