all repos — h3rald @ 248e5490fcd4d6a18b54dfac38f6fe1c7a074ce4

The sources of https://h3rald.com

content/ruby-compendium/book/syntax.html

 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
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
<!DOCTYPE html> 
<html lang="en"> 
	<head> 
    <title>Syntax - Glyph</title> 
		
		<meta charset="utf-8" />
		<meta name="author" content="Fabio Cevasco" />
		<meta name="copyright" content="Fabio Cevasco" />
		<meta name="robots" content="all, follow" />
		<meta name="Revisit-After" content="2 Days" />
		<meta name="language" content="en" />
		<meta name="target_country" content="en-us" />
		<meta name="country" content="United States" />
                <meta name="readability-verification" content="XmaVzRjmcFL5Bb2Fu9TZVdDKzhA3apQdxexTB2rK"/>
		<meta name="description" content="H3RALD - Fabio Cevasco's Web Site" />
		
		<meta name="keywords" content="h3rald, fabio cevasco, glyph" />
		<link rel="shortcut icon" href="/favicon.png" type="image/png" /> 
		<meta content="44.388041;9.073248" name="ICBM" />

		<link rel="stylesheet" type="text/css" href="/styles/html5reset.css" />
		<link rel="stylesheet" type="text/css" href="/styles/style.css" />

    <script src="http://www.google.com/jsapi?key=ABQIAAAAr6RY1Z6dchG_sX9WDLSy3xRlq2n1sm52B5HDRR5tm6o8XM18FhR56xHNNH6CsX86uN5VoTrglpyOyQ" type="text/javascript"></script>
		<script src="/js/sevenup.js" type="text/javascript"></script>
		<script src="/js/sevenup_black.js" type="text/javascript"></script>
		<script type="text/javascript">google.load("jquery", "1");</script>
		<script src="/js/jquery-timeago.js" type="text/javascript"></script>
		<script src="/js/jquery-easing.js" type="text/javascript"></script>
		<script src="/js/jquery-fancybox.js" type="text/javascript"></script>
		<script src="/js/date.js" type="text/javascript"></script>
		<script src="/js/feeds.js" type="text/javascript"></script>
		<script src="/js/hyphenator.js" type="text/javascript"></script>
		<script src="/js/init.js" type="text/javascript"></script>
		<script src="/js/search.js" type="text/javascript"></script>
	</head>
	<body>
		<section id="container">
			
				<header class="page">
	<nav class="home-link">
		<a href="/">
      <span class="logo">
        <span class="hrald"><span class="h">H</span>&nbsp;<span class="rald">RALD</span></span>
        <span class="three">3</span>
      </span>
		</a>
	</nav>
	
	<nav class="section">
		/<a href="/glyph/" rel="glyph">GLYPH</a>
	</nav>
	
</header>
				<article class="page glyph-book">
					<header>
	<hgroup>
		<h1>Syntax</h1> 
		<h2>Glyph</h2>
	</hgroup>
  </header>
					<section id="body-text" class="hyphenate glyph-book">

							<nav class="navigation"><a href="/ruby-compendium/book/implementations.html">← Versions and Implementations</a> | <a href="/ruby-compendium/book/index.html">Contents</a> | <a href="/ruby-compendium/book/core-stdlib.html">The Core and the Standard Library →</a></nav>
							<p>
Teaching you how to program in Ruby goes beyond the scope of this book, however, this section will show you at least what Ruby code looks like. If you know another programming language already, some things may already be familiar to you. If you don't, hopefully the following code will not appear too intimidating.
</p>
<div class="CodeRay">
  <div class="code"><pre><span class="line-numbers"> <a href="#n1" name="n1">1</a></span><span class="comment"># This is a comment and will not be executed </span>
<span class="line-numbers"> <a href="#n2" name="n2">2</a></span><span class="comment"># by the Ruby interpreter.</span>
<span class="line-numbers"> <a href="#n3" name="n3">3</a></span><span class="comment">#</span>
<span class="line-numbers"> <a href="#n4" name="n4">4</a></span><span class="comment"># This is not the usual 'Hello World' example, so </span>
<span class="line-numbers"> <a href="#n5" name="n5">5</a></span><span class="comment"># don't worry if you don't understand everything. </span>
<span class="line-numbers"> <a href="#n6" name="n6">6</a></span><span class="comment"># This example is meant to give you a general feeling </span>
<span class="line-numbers"> <a href="#n7" name="n7">7</a></span><span class="comment"># of what it is like to write Ruby programs.</span>
<span class="line-numbers"> <a href="#n8" name="n8">8</a></span>
<span class="line-numbers"> <a href="#n9" name="n9">9</a></span>require <span class="string"><span class="delimiter">'</span><span class="content">pathname</span><span class="delimiter">'</span></span>   <span class="comment"># Here we're requiring an external library</span>
<span class="line-numbers"><strong><a href="#n10" name="n10">10</a></strong></span>                     <span class="comment"># which is part of the Ruby Standard Library.</span>
<span class="line-numbers"><a href="#n11" name="n11">11</a></span>
<span class="line-numbers"><a href="#n12" name="n12">12</a></span><span class="keyword">class</span> <span class="class">FilePrinter</span>
<span class="line-numbers"><a href="#n13" name="n13">13</a></span>
<span class="line-numbers"><a href="#n14" name="n14">14</a></span>  <span class="comment"># Constructor method</span>
<span class="line-numbers"><a href="#n15" name="n15">15</a></span>  <span class="keyword">def</span> <span class="function">initialize</span>(path)
<span class="line-numbers"><a href="#n16" name="n16">16</a></span>    <span class="comment"># This method expects a valid path, however Ruby is dynamically-typed</span>
<span class="line-numbers"><a href="#n17" name="n17">17</a></span>    <span class="comment"># so anything can be passed to this method.</span>
<span class="line-numbers"><a href="#n18" name="n18">18</a></span>    <span class="comment"># To check that the input value is valid, just check if if it behaves</span>
<span class="line-numbers"><a href="#n19" name="n19">19</a></span>    <span class="comment"># like a path. This is called 'duck typing'.</span>
<span class="line-numbers"><strong><a href="#n20" name="n20">20</a></strong></span>    raise <span class="constant">RuntimeError</span>, <span class="string"><span class="delimiter">&quot;</span><span class="content">Invalid path: </span><span class="inline"><span class="inline-delimiter">#{</span>path<span class="inline-delimiter">}</span></span><span class="delimiter">&quot;</span></span> <span class="keyword">unless</span> path.respond_to? <span class="symbol">:basename</span>
<span class="line-numbers"><a href="#n21" name="n21">21</a></span>    <span class="instance-variable">@path</span> = path
<span class="line-numbers"><a href="#n22" name="n22">22</a></span>    <span class="instance-variable">@name</span> = <span class="instance-variable">@path</span>.basename
<span class="line-numbers"><a href="#n23" name="n23">23</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n24" name="n24">24</a></span>
<span class="line-numbers"><a href="#n25" name="n25">25</a></span>  <span class="keyword">def</span> <span class="function">show</span>
<span class="line-numbers"><a href="#n26" name="n26">26</a></span>    <span class="comment"># Ruby objects and expressions can be interpolated in strings</span>
<span class="line-numbers"><a href="#n27" name="n27">27</a></span>    puts <span class="string"><span class="delimiter">&quot;</span><span class="content">  </span><span class="delimiter">#</span><span class="instance-variable">@name</span><span class="content"> -- </span><span class="inline"><span class="inline-delimiter">#{</span><span class="instance-variable">@path</span>.stat.size<span class="inline-delimiter">}</span></span><span class="content"> bytes</span><span class="delimiter">&quot;</span></span>
<span class="line-numbers"><a href="#n28" name="n28">28</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n29" name="n29">29</a></span>
<span class="line-numbers"><strong><a href="#n30" name="n30">30</a></strong></span><span class="keyword">end</span>
<span class="line-numbers"><a href="#n31" name="n31">31</a></span>
<span class="line-numbers"><a href="#n32" name="n32">32</a></span><span class="keyword">class</span> <span class="class">DirPrinter</span> &lt; <span class="constant">FilePrinter</span> <span class="comment"># Definition of a child class</span>
<span class="line-numbers"><a href="#n33" name="n33">33</a></span>
<span class="line-numbers"><a href="#n34" name="n34">34</a></span>  <span class="keyword">def</span> <span class="function">initialize</span>(path)
<span class="line-numbers"><a href="#n35" name="n35">35</a></span>    <span class="keyword">super</span> <span class="comment"># Call to the parent's constructur</span>
<span class="line-numbers"><a href="#n36" name="n36">36</a></span>    <span class="instance-variable">@children</span> = <span class="instance-variable">@path</span>.children
<span class="line-numbers"><a href="#n37" name="n37">37</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n38" name="n38">38</a></span>
<span class="line-numbers"><a href="#n39" name="n39">39</a></span>  <span class="keyword">def</span> <span class="function">show</span>
<span class="line-numbers"><strong><a href="#n40" name="n40">40</a></strong></span>    puts <span class="string"><span class="delimiter">&quot;</span><span class="content">  </span><span class="delimiter">#</span><span class="instance-variable">@name</span><span class="content">/ -- </span><span class="inline"><span class="inline-delimiter">#{</span><span class="instance-variable">@children</span>.length<span class="inline-delimiter">}</span></span><span class="content"> item(s)</span><span class="delimiter">&quot;</span></span>
<span class="line-numbers"><a href="#n41" name="n41">41</a></span>  <span class="keyword">end</span>
<span class="line-numbers"><a href="#n42" name="n42">42</a></span>
<span class="line-numbers"><a href="#n43" name="n43">43</a></span><span class="keyword">end</span>
<span class="line-numbers"><a href="#n44" name="n44">44</a></span>
<span class="line-numbers"><a href="#n45" name="n45">45</a></span><span class="comment"># No parenthesis are required unless needed!</span>
<span class="line-numbers"><a href="#n46" name="n46">46</a></span>pwd = <span class="constant">Pathname</span>.new <span class="constant">Dir</span>.pwd
<span class="line-numbers"><a href="#n47" name="n47">47</a></span>
<span class="line-numbers"><a href="#n48" name="n48">48</a></span>puts <span class="string"><span class="delimiter">&quot;</span><span class="content">Current Directory: </span><span class="inline"><span class="inline-delimiter">#{</span>pwd<span class="inline-delimiter">}</span></span><span class="delimiter">&quot;</span></span>
<span class="line-numbers"><a href="#n49" name="n49">49</a></span>
<span class="line-numbers"><strong><a href="#n50" name="n50">50</a></strong></span><span class="comment"># Get the children items of the current directory, </span>
<span class="line-numbers"><a href="#n51" name="n51">51</a></span><span class="comment"># select only directories, </span>
<span class="line-numbers"><a href="#n52" name="n52">52</a></span><span class="comment"># sort them alphabetically,</span>
<span class="line-numbers"><a href="#n53" name="n53">53</a></span><span class="comment"># and for each one of them...</span>
<span class="line-numbers"><a href="#n54" name="n54">54</a></span>pwd.children.select{|i| i.directory? }.sort.each <span class="keyword">do</span> |item|
<span class="line-numbers"><a href="#n55" name="n55">55</a></span>  <span class="comment"># Call the show method, printing the </span>
<span class="line-numbers"><a href="#n56" name="n56">56</a></span>  <span class="comment"># directory name and the number of child items</span>
<span class="line-numbers"><a href="#n57" name="n57">57</a></span>  <span class="constant">DirPrinter</span>.new(item).show
<span class="line-numbers"><a href="#n58" name="n58">58</a></span><span class="keyword">end</span>
<span class="line-numbers"><a href="#n59" name="n59">59</a></span>
<span class="line-numbers"><strong><a href="#n60" name="n60">60</a></strong></span>pwd.children.select{|i| !i.directory? }.sort.each <span class="keyword">do</span> |item|
<span class="line-numbers"><a href="#n61" name="n61">61</a></span>  <span class="constant">FilePrinter</span>.new(item).show
<span class="line-numbers"><a href="#n62" name="n62">62</a></span><span class="keyword">end</span></pre></div>
</div>

<aside class="box">
<div class="box-title">Trying out Ruby...</div>
If your hands are itching to try writing Ruby code, but you don't want to install it just yet, head over to <a href="http://tryruby.org/">Try Ruby!</a>, a unique way to try the language right in your browser. Or, if you already installed Ruby, follow the <a href="http://www.ruby-lang.org/en/documentation/quickstart/">Ruby in 20 minutes</a> tutorial, using Interactive RuBy (IRB).

</aside>
							<nav class="navigation"><a href="/ruby-compendium/book/implementations.html">← Versions and Implementations</a> | <a href="/ruby-compendium/book/index.html">Contents</a> | <a href="/ruby-compendium/book/core-stdlib.html">The Core and the Standard Library →</a></nav>
										<nav id="sharing-buttons">
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="h3rald">Tweet</a>
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</nav>
					</section>
				</article>
					
			
			<footer>
				<section>
					<nav>
						<a href="/about/">ABOUT</a>|<a href="/contact/">CONTACT</a>
					</nav>
					<p>H3RALD Web Site v8.1.2 &copy; 2004 &mdash; 2012 <em>Fabio Cevasco</em></p>
				</section>
			</footer>
		</section><!-- #container end -->
		
		
 <!-- Start Google Analytics --> 
 <script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18587377-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
 </script> 
 <!-- End Google Analytics --> 
 <!-- Start of StatCounter Code --> 
  <script type="text/javascript"> 
	var sc_project=6193656; 
	var sc_invisible=1; 
	var sc_security="57f7ee2a"; 
	</script> 
	<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script> 
	<!-- End of StatCounter Code --> 
	</body>
</html>