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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
<!DOCTYPE html> <html lang="en"> <head> <title>Syntax – Ruby Compendium</title> <link href='http://fonts.googleapis.com/css?family=Crimson+Text:400,400italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'> <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, ruby-compendium" /> <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 src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></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="/"> <img src="/images/h3rald_small.png" alt="H3RALD" class="default"/> <![if !IE]> <img src="/images/h3rald_hover_small.png" alt="H3RALD" class="hover"/> <![endif]> </a> </nav> <nav class="section"> /<a href="/ruby-compendium/" rel="ruby-compendium">RUBY-COMPENDIUM</a> </nav> </header> <article class="page glyph-book"> <header> <hgroup> <h1>Syntax</h1> <h2>Ruby Compendium</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="no"> 1</span> <span class="c"># This is a comment and will not be executed </span> <span class="no"> 2</span> <span class="c"># by the Ruby interpreter.</span> <span class="no"> 3</span> <span class="c">#</span> <span class="no"> 4</span> <span class="c"># This is not the usual 'Hello World' example, so </span> <span class="no"> 5</span> <span class="c"># don't worry if you don't understand everything. </span> <span class="no"> 6</span> <span class="c"># This example is meant to give you a general feeling </span> <span class="no"> 7</span> <span class="c"># of what it is like to write Ruby programs.</span> <span class="no"> 8</span> <span class="no"> 9</span> require <span class="s"><span class="dl">'</span><span class="k">pathname</span><span class="dl">'</span></span> <span class="c"># Here we're requiring an external library</span> <span class="no"><strong>10</strong></span> <span class="c"># which is part of the Ruby Standard Library.</span> <span class="no">11</span> <span class="no">12</span> <span class="r">class</span> <span class="cl">FilePrinter</span> <span class="no">13</span> <span class="no">14</span> <span class="c"># Constructor method</span> <span class="no">15</span> <span class="r">def</span> <span class="fu">initialize</span>(path) <span class="no">16</span> <span class="c"># This method expects a valid path, however Ruby is dynamically-typed</span> <span class="no">17</span> <span class="c"># so anything can be passed to this method.</span> <span class="no">18</span> <span class="c"># To check that the input value is valid, just check if if it behaves</span> <span class="no">19</span> <span class="c"># like a path. This is called 'duck typing'.</span> <span class="no"><strong>20</strong></span> raise <span class="co">RuntimeError</span>, <span class="s"><span class="dl">"</span><span class="k">Invalid path: </span><span class="il"><span class="idl">#{</span>path<span class="idl">}</span></span><span class="dl">"</span></span> <span class="r">unless</span> path.respond_to? <span class="sy">:basename</span> <span class="no">21</span> <span class="iv">@path</span> = path <span class="no">22</span> <span class="iv">@name</span> = <span class="iv">@path</span>.basename <span class="no">23</span> <span class="r">end</span> <span class="no">24</span> <span class="no">25</span> <span class="r">def</span> <span class="fu">show</span> <span class="no">26</span> <span class="c"># Ruby objects and expressions can be interpolated in strings</span> <span class="no">27</span> puts <span class="s"><span class="dl">"</span><span class="k"> </span><span class="dl">#</span><span class="iv">@name</span><span class="k"> -- </span><span class="il"><span class="idl">#{</span><span class="iv">@path</span>.stat.size<span class="idl">}</span></span><span class="k"> bytes</span><span class="dl">"</span></span> <span class="no">28</span> <span class="r">end</span> <span class="no">29</span> <span class="no"><strong>30</strong></span> <span class="r">end</span> <span class="no">31</span> <span class="no">32</span> <span class="r">class</span> <span class="cl">DirPrinter</span> < <span class="co">FilePrinter</span> <span class="c"># Definition of a child class</span> <span class="no">33</span> <span class="no">34</span> <span class="r">def</span> <span class="fu">initialize</span>(path) <span class="no">35</span> <span class="r">super</span> <span class="c"># Call to the parent's constructur</span> <span class="no">36</span> <span class="iv">@children</span> = <span class="iv">@path</span>.children <span class="no">37</span> <span class="r">end</span> <span class="no">38</span> <span class="no">39</span> <span class="r">def</span> <span class="fu">show</span> <span class="no"><strong>40</strong></span> puts <span class="s"><span class="dl">"</span><span class="k"> </span><span class="dl">#</span><span class="iv">@name</span><span class="k">/ -- </span><span class="il"><span class="idl">#{</span><span class="iv">@children</span>.length<span class="idl">}</span></span><span class="k"> item(s)</span><span class="dl">"</span></span> <span class="no">41</span> <span class="r">end</span> <span class="no">42</span> <span class="no">43</span> <span class="r">end</span> <span class="no">44</span> <span class="no">45</span> <span class="c"># No parenthesys are required unless needed!</span> <span class="no">46</span> pwd = <span class="co">Pathname</span>.new <span class="co">Dir</span>.pwd <span class="no">47</span> <span class="no">48</span> puts <span class="s"><span class="dl">"</span><span class="k">Current Directory: </span><span class="il"><span class="idl">#{</span>pwd<span class="idl">}</span></span><span class="dl">"</span></span> <span class="no">49</span> <span class="no"><strong>50</strong></span> <span class="c"># Get the children items of the current directory, </span> <span class="no">51</span> <span class="c"># select only directories, </span> <span class="no">52</span> <span class="c"># sort them alphabetically,</span> <span class="no">53</span> <span class="c"># and for each one of them...</span> <span class="no">54</span> pwd.children.select{|i| i.directory? }.sort.each <span class="r">do</span> |item| <span class="no">55</span> <span class="c"># Call the show method, printing the </span> <span class="no">56</span> <span class="c"># directory name and the number of child items</span> <span class="no">57</span> <span class="co">DirPrinter</span>.new(item).show <span class="no">58</span> <span class="r">end</span> <span class="no">59</span> <span class="no"><strong>60</strong></span> pwd.children.select{|i| !i.directory? }.sort.each <span class="r">do</span> |item| <span class="no">61</span> <span class="co">FilePrinter</span>.new(item).show <span class="no">62</span> <span class="r">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> <g:plusone size="medium"></g:plusone> <script type="text/javascript"> (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script> </nav> </section> </article> <footer> <section class="ads"> <script type="text/javascript"><!-- google_ad_client = "pub-2871497824158668"; /* 728x90, created 9/10/10 */ google_ad_slot = "3963343166"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </section> <section> <nav> <a href="/about/">ABOUT</a>|<a href="/contact/">CONTACT</a> </nav> <p>H3RALD Web Site v8.1.2 © 2004 — 2011 <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> |