-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby_usage_notes.html
More file actions
92 lines (85 loc) · 4.77 KB
/
ruby_usage_notes.html
File metadata and controls
92 lines (85 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<html>
<head>
<title>Ruby usage notes</title>
</head>
<h1>Ruby notes</h1>
<h2>Command execution</h2>
<ol>
<li>Simplest way to run a ruby script: type <code>ruby</code> into a terminal, enter a command then <strong>^D</strong> to execute.</li>
<li>The eval.rb script - in the distribution code, sample folder, provides a program showing the value of each expression as it is entered.</li>
<li>Note, when executing a command like <code>`puts`</code>, output is displayed and the return too, (in the case of puts =>nil).</li>
<li>The irb program provides an interactive, multiline Ruby shell: complete with command line history, line editing capabilities and job control.</li>
<li>Run a ruby script as a program simply with, ruby programName.rb</li>
<li>...or on Unix use the shebang notation, specifying at the top of the file, <code>#!/usr/local/bin/ruby -w</code> (or, if the O/S allows it, using <code>#!/usr/bin/env ruby</code>, which will search the path for ruby and then execute it).</li>
</ol>
<h2>Objects</h2>
<p>
New up an object with the <code>new()</code> keyword/constructor. The syntax for calling a constructor on a class, passing in a parameter and assigning a pointer looks something like,
<code>song1=Song.new("Deep forbidden lake") // note no end of line semi-colon</code>
<br/>
Similarly length() can be applied directly as an operator on an object instance,
<code>name.length</code>. No need to call a separate function on a string to get information about that string!
Follow <a href="ruby_objects.html">this link<a/> for more about detail on objects in Ruby.
</p>
<h2>Numbers, arithmetic etc</h2>
Numbers act like objects and have intrinsic objects built-in. So, for example, it's possible to get the absolute value of an irrational number simply by entering,
<code>numbera = Number.new(1234.32)</code>
<code>numbera.abs()</code>
<h2>Strings</h2>
Note, inside quotes, "#{expression}" is considered to be an expression, which can be replaced following evaluion of that expression.
<h2>Names</h2>
Ruby convention = <i>first characters of a name indicate its use</i>
<ol>
<li><b>Local variables</b>, <b>method parameters</b>, <b>method names</b> : lowercase letter or underscore '_'</li>
<li><b>Global variables</b> : prefixed with a dollar '$'</li>
<li><b>Instance variables</b> : at the rate sign '@'</li>
<li><b>Class variables</b> : two at the rate signs '@@'</li>
<li><b>Class names, module names</b> and <b>constants</b> : upper case letter</li>
</ol>
Following the initial character a name can be any combination of letters, digits, and underscores (though a character following an at the rate '@' character maay not be a digit).
<h2>Arrays and Hashes</h2>
<p>Indexed collections - accessible via a key. A ruby array index is an <b>integer</b> while hashes support any object as a key. Both arrays and hashes can grow dynamically and both can hold objects of different types.</p>
<h3>Arrays</h3>
<p>New arrays can be created with an array literal <code>empty1 = []</code> or using the 'new' keyword, <code> empty2 = Array.new</code></p>
<h3>Hashes</h3>
<p>A hash literal uses braces rather than square brackets. <code>=></code> is used as the assignment syntax, with commas between hash elements.
<code><pre>
AinstSection = {
'cello' => 'string',
'clarinet' => 'woodwind',
'drum' => 'percussion',
'oboe' => 'woodwind',
'trumpet' => 'brass',
'violin' => 'string'
}
</pre></code>
<p>Returns nil when indexed by a key it doesn't contain. <i>Note! Nil means false in a conditional expression</i> but this default return value (for when a key is not found) can be set on initialisation.</p>
<p></p>
<h2>Control Structures</h2>
<p>Ruby uses the keyword <code>end</code> to signify the end of code block.</p>
<p>If an expression fits on one line, a 'statement modifier' may be used, for example</p>
<p><code>puts "some text" if count > 10</code>, or </p>
<p><code>square = square*square while square < 1000</code></p>
<h2>Regular expressions</h2>
<p>
...are built into Ruby.
</p>
<p>
The match operator =~ in Ruby will return the starting position of the matched string, or nil if no match is found.
</p>
<h2>Blocks and Iterators</h2>
<p>
Blocks are a particular strength of Ruby - they can be passed to method invocations almost like parameters and marry the benefits of a dynamically typed language. They can be used to implement callbacks, pass around chunks of code and to implement iterators.
Methods that invoke <code>yield</code>, that have themselves been called with a code block passed in, constitute a <i>co-routine</i>
yield and calls to blocks. Here's an example,
<code>
def callBlock
yield
yield
end
callBlock { puts "In the block" }
</code>
</p>
<h2>Inspect</h2>
The {code}inspect{code} message can be sent to any object and will, "...dumps out the object's id and instance variables."
</html>