CINXE.COM
Menguasai Ruby dalam 20 Menit
<!DOCTYPE html> <html> <head><script type="text/javascript" src="/_static/js/bundle-playback.js?v=HxkREWBo" charset="utf-8"></script> <script type="text/javascript" src="/_static/js/wombat.js?v=txqj7nKC" charset="utf-8"></script> <script>window.RufflePlayer=window.RufflePlayer||{};window.RufflePlayer.config={"autoplay":"on","unmuteOverlay":"hidden"};</script> <script type="text/javascript" src="/_static/js/ruffle/ruffle.js"></script> <script type="text/javascript"> __wm.init("https://web.archive.org/web"); __wm.wombat("https://www.ruby-lang.org/id/documentation/quickstart/","20240802231934","https://web.archive.org/","web","/_static/", "1722640774"); </script> <link rel="stylesheet" type="text/css" href="/_static/css/banner-styles.css?v=S1zqJCYt" /> <link rel="stylesheet" type="text/css" href="/_static/css/iconochive.css?v=3PDvdIFv" /> <!-- End Wayback Rewrite JS Include --> <meta charset="utf-8"> <title>Menguasai Ruby dalam 20 Menit</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta itemprop="image" content="https://web.archive.org/web/20240802231934im_/https://www.ruby-lang.org/images/header-ruby-logo@2x.png"> <meta name="description" content=""> <link rel="stylesheet" type="text/css" href="/web/20240802231934cs_/https://www.ruby-lang.org/stylesheets/normalize.css"> <link rel="stylesheet" type="text/css" href="/web/20240802231934cs_/https://www.ruby-lang.org/stylesheets/main.css"> <link rel="stylesheet" type="text/css" href="/web/20240802231934cs_/https://www.ruby-lang.org/stylesheets/pygments.css"> <link rel="stylesheet" type="text/css" href="/web/20240802231934cs_/https://www.ruby-lang.org/stylesheets/mobile.css"> <link rel="stylesheet" type="text/css" href="/web/20240802231934cs_/https://www.ruby-lang.org/stylesheets/print.css"> <link href="https://web.archive.org/web/20240802231934cs_/https://fonts.googleapis.com/css?family=Noto+Sans:400,700,400italic,700italic&subset=latin,cyrillic,greek,vietnamese" rel="stylesheet" type="text/css"> <link rel="canonical" href="https://web.archive.org/web/20240802231934/https://www.ruby-lang.org/id/documentation/quickstart/"> <link rel="shortcut icon" type="image/x-icon" href="/web/20240802231934im_/https://www.ruby-lang.org/favicon.ico"> <link href="/web/20240802231934/https://www.ruby-lang.org/id/feeds/news.rss" rel="alternate" title="Umpan Web (RSS)" type="application/rss+xml"> <script type="text/javascript" src="/web/20240802231934js_/https://www.ruby-lang.org/javascripts/jquery.min.js"></script> <script type="text/javascript" src="/web/20240802231934js_/https://www.ruby-lang.org/javascripts/page.js"></script> </head> <body> <div id="header"> <div id="header_content" class="container"> <a href="/web/20240802231934/https://www.ruby-lang.org/id/"> <h1>Ruby</h1> <h2>Sahabat Terbaik Programmer</h2> </a> <div class="site-links"> <a href="/web/20240802231934/https://www.ruby-lang.org/id/" class="home">Beranda</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/downloads/">Unduh</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/">Dokumentasi</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/libraries/">Pustaka</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/community/">Komunitas</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/news/">Berita</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/security/">Keamanan</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/about/">Tentang Ruby</a> <a href="#" class="menu selected">Menu</a> </div> <div id="search-box"> <form id="search-form" action="https://web.archive.org/web/20240802231934/https://www.google.com/cse"> <table class="fieldset"> <tr> <td> <input class="field" type="text" name="q" size="31" style="background: white url(//web.archive.org/web/20240802231934im_/https://www.google.com/coop/intl/id/images/google_custom_search_watermark.gif) left no-repeat" onfocus="this.style.background='white'" onblur="if (/^\s*$/.test(this.value)) this.style.background='white url(//www.google.com/coop/intl/id/images/google_custom_search_watermark.gif) left no-repeat'"/> </td> <td> <input type="hidden" name="cx" value="013598269713424429640:g5orptiw95w"/> <input type="hidden" name="ie" value="UTF-8"/> <input class="button" type="submit" name="sa" value="Cari"/> </td> </tr> </table> </form> </div> </div> </div> <div id="page"> <div id="main-wrapper" class="container"> <div id="main"> <div id="content-wrapper"> <div class="multi-page"> <strong>1</strong> <span class="separator"> | </span> <a href="2/" title="Bagian 2">2</a> <span class="separator"> | </span> <a href="3/" title="Bagian 3">3</a> <span class="separator"> | </span> <a href="4/" title="Bagian 4">4</a> </div> <h1>Menguasai Ruby dalam 20 Menit</h1> <div id="content"> <h2>Pengantar</h2> <p>Ini adalah sebuah tutorial mini Ruby yang bisa diselesaikan tidak lebih dari 20 menit. Dengan asumsi Anda sudah menginstal Ruby. (Jika Anda belum menginstal Ruby pada komputer Anda, <a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/installation/">instalasi</a> terlebih dahulu sebelum Anda memulai.</p> <h2>Ruby Interaktif</h2> <p>Ruby datang dengan sebuah program yang akan menampilkan hasil dari <em>statement</em> Ruby apapun saat Anda memasukkannya. Bermain dengan kode Ruby pada sesi interaktif seperti ini adalah salah satu cara terbaik untuk belajar bahasa pemograman.</p> <p>Buka IRB (singkatan dari <em>Interactive Ruby</em>).</p> <ul> <li>Jika Anda menggunakan <strong>macOS</strong> buka <code class="language-plaintext highlighter-rouge">Terminal</code> dan ketik <code class="language-plaintext highlighter-rouge">irb</code>, kemudian tekan <em>enter</em>.</li> <li>Jika Anda menggunakan <strong>Linux</strong>, buka <em>shell</em> dan ketik <code class="language-plaintext highlighter-rouge">irb</code>, kemudian tekan <em>enter</em>.</li> <li>Jika Anda menggunakan <strong>Windows</strong>, buka <code class="language-plaintext highlighter-rouge">Interactive Ruby</code> dari bagian Ruby pada <em>Start Menu</em>.</li> </ul> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):001:0></span></code></pre></figure> <p>Baik, sekarang irb sudah dibuka. Lalu selanjutnya?</p> <p>Ketik ini: <code class="language-plaintext highlighter-rouge">"Hello World"</code></p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):001:0></span><span class="w"> </span><span class="s2">"Hello World"</span> <span class="p">=></span> <span class="s2">"Hello World"</span></code></pre></figure> <h2>Ruby Mematuhi Anda!</h2> <p>Apa yang baru saja terjadi? Apakah kita baru saja menulis program tersingkat di dunia berupa “Hello World”? Tidak persis begitu. Baris kedua hanya sekedar cara IRB memberitahu hasil dari ekspresi terakhir yang dievaluasi/dijalankan. Jika kita ingin mencetak “Hello World” kita perlu tambahkan sedikit:</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):002:0></span><span class="w"> </span><span class="nb">puts</span> <span class="s2">"Hello World"</span> <span class="go">Hello World </span><span class="p">=></span> <span class="kp">nil</span></code></pre></figure> <p><code class="language-plaintext highlighter-rouge">puts</code> merupakan perintah dasar untuk mencetak sesuatu pada Ruby. Kemudian apa artinya <code class="language-plaintext highlighter-rouge">=> nil</code>? Artinya adalah hasil dari ekspresi. <code class="language-plaintext highlighter-rouge">puts</code> selalu mengembalikan nil, yang merupakan <em>absolutely-positively-nothing value</em> pada Ruby.</p> <h2>Kalkulator Gratis Anda Di Sini</h2> <p>Dengan IRB kita sudah cukup mempunyai kalkulator dasar:</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):003:0></span><span class="w"> </span><span class="mi">3</span><span class="o">+</span><span class="mi">2</span> <span class="p">=></span> <span class="mi">5</span></code></pre></figure> <p>Tiga tambah dua. Cukup mudah. Bagaimana dengan tiga <em>kali</em> dua? Anda bisa mencoba dengan langsung mengetikkannya, cukup pendek, tetapi Anda juga bisa mengganti apa saja yang baru saja Anda perintahkan di IRB. Coba tekan <strong>panah-atas</strong> pada <em>keyboard</em> Anda dan lihatlah apakah IRB memunculkan baris <code class="language-plaintext highlighter-rouge">3+2</code> lagi. Jika Ya, Anda dapat menggunakan panah-kiri untuk berpindah ke setelah tanda <code class="language-plaintext highlighter-rouge">+</code> kemudian gunakan <em>backspace</em> untuk menggantinya dengan tanda <code class="language-plaintext highlighter-rouge">*</code>.</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):004:0></span><span class="w"> </span><span class="mi">3</span><span class="o">*</span><span class="mi">2</span> <span class="p">=></span> <span class="mi">6</span></code></pre></figure> <p>Lanjut, mari kita coba tiga kuadrat:</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):005:0></span><span class="w"> </span><span class="mi">3</span><span class="o">**</span><span class="mi">2</span> <span class="p">=></span> <span class="mi">9</span></code></pre></figure> <p>Di Ruby, <code class="language-plaintext highlighter-rouge">**</code> merupakan cara Anda mengatakan “pangkat dari”. Tetapi bagaimana jika Anda ingin melakukannya dengan cara lain dan mendapatkan akar dari suatu bilangan?</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):006:0></span><span class="w"> </span><span class="no">Math</span><span class="p">.</span><span class="nf">sqrt</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span> <span class="p">=></span> <span class="mf">3.0</span></code></pre></figure> <p>Baik, tunggu dulu, bagaimana yang terakhir itu? Jika Anda menebak, “Kode tersebut mencari sqrt (akar) dari 9”, berarti Anda benar. Tetapi mari kita lihat lebih dekat. Pertama, apa itu <code class="language-plaintext highlighter-rouge">Math</code>?</p> <h2>Module, Pengelompokan Kode Menurut Topik</h2> <p><code class="language-plaintext highlighter-rouge">Math</code> merupakan <em>built-in</em> (bawaan) <em>module</em> untuk matematika. <em>Module</em> memiliki dua tugas pada Ruby. Pertama, mengelompokkan <em>method</em> yang memiliki kemiripan fungsi ke dalam sebuah nama yang sama. <code class="language-plaintext highlighter-rouge">Math</code> juga punya <em>method</em>, seperti <code class="language-plaintext highlighter-rouge">sin()</code> dan <code class="language-plaintext highlighter-rouge">tan()</code>.</p> <p>Selanjutnya adalah <em>dot</em> (titik). Apa yang <em>dot</em> lakukan? Dengan <em>dot</em>-lah, Anda bisa mengidentifikasi <em>receiver</em> dari suatu <em>message</em>. Nah, kalau <em>message</em> apa? Dalam hal ini, <em>message</em> adalah <code class="language-plaintext highlighter-rouge">sqrt(9)</code>, yang artinya pemanggilan <em>method</em> <code class="language-plaintext highlighter-rouge">sqrt</code>, kependekan dari “square root” dengan parameter <code class="language-plaintext highlighter-rouge">9</code>.</p> <p>Hasil dari pemanggilan <em>method</em> ini adalah nilai <code class="language-plaintext highlighter-rouge">3.0</code>. Anda bisa perhatikan nilai bukan cuma <code class="language-plaintext highlighter-rouge">3</code> (tanpa <code class="language-plaintext highlighter-rouge">.0</code>). Itu karena akar dari sebuah bilangan sering bukan berupa bilangan bulat (<em>integer</em>), sehingga <em>method</em> ini selalu mengembalikan bilangan desimal (<em>floating-point number</em>).</p> <p>Bagaimana jika kita ingin mengingat hasil dari beberapa operasi matematika ini? Caranya simpan hasilnya ke sebuah variabel.</p> <figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):007:0></span><span class="w"> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">**</span> <span class="mi">2</span> <span class="p">=></span> <span class="mi">9</span> <span class="gp">irb(main):008:0></span><span class="w"> </span><span class="n">b</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">**</span> <span class="mi">2</span> <span class="p">=></span> <span class="mi">16</span> <span class="gp">irb(main):009:0></span><span class="w"> </span><span class="no">Math</span><span class="p">.</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span> <span class="p">=></span> <span class="mf">5.0</span></code></pre></figure> <p>Sehebat kalkulator, kita sudah pergi dari pesan <code class="language-plaintext highlighter-rouge">Hello World</code> yang biasanya selalu saja kalau ada tutorial untuk pemula berfokus di sana… <a href="2/">jadi mari kita kembali ke sana</a>.</p> </div> </div> <hr class="hidden-modern"/> <div id="sidebar-wrapper"> <div id="sidebar"> <div class="navigation"> <h3><strong>Coba Sekarang</strong>, sangat mudah!</h3> <ul class="menu"> <li><a href="https://web.archive.org/web/20240802231934/https://try.ruby-lang.org/">Try Ruby! (langsung di browser Anda)</a></li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/quickstart/">Menguasai Ruby dalam 20 Menit</a></li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/ruby-from-other-languages/">Dari Bahasa Pemrograman Lain ke Ruby</a></li> </ul> </div> <div class="navigation"> <h3><strong>Jelajahi</strong> sebuah dunia baru…</h3> <ul class="menu"> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/">Dokumentasi</a></li> <li><a href="https://web.archive.org/web/20240802231934/https://rubybib.org/">Penelitian Akademis</a></li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/libraries/">Pustaka</a></li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/success-stories/">Kisah Sukses</a></li> </ul> </div> <div class="navigation"> <h3><strong>Bergabunglah</strong> dalam komunitas yang ramah dan makin berkembang.</h3> <ul> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/community/mailing-lists/">Mailing List</a>: Berdiskusi dengan programmer Ruby dari Indonesia dan seluruh dunia.</li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/community/user-groups/">Komunitas Pengguna</a>: Berpartisipasilah dengan Rubyist di lokasi Anda.</li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/community/weblogs/">Blog</a>: Ulasan terbaru dari komunitas Ruby.</li> <li><a href="/web/20240802231934/https://www.ruby-lang.org/id/community/ruby-core/">Ruby Core</a>: Silakan berkontribusi untuk versi Ruby terbaru.</li> <li><a href="https://web.archive.org/web/20240802231934/https://bugs.ruby-lang.org/">Laporkan Masalah</a>: Lapor atau bantu memecahkan masalah yang ada di Ruby.</li> </ul> </div> <h3>Sindikasi Web</h3> <p><a href="/web/20240802231934/https://www.ruby-lang.org/id/feeds/news.rss">Umpan Web (RSS)</a></p> </div> </div> <hr class="hidden-modern"/> </div> </div> </div> <div class="container"> <div id="footer"> <div class="site-links"> <a href="/web/20240802231934/https://www.ruby-lang.org/id/" class="home">Beranda</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/downloads/">Unduh</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/documentation/">Dokumentasi</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/libraries/">Pustaka</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/community/">Komunitas</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/news/">Berita</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/security/">Keamanan</a> <a href="/web/20240802231934/https://www.ruby-lang.org/id/about/">Tentang Ruby</a> </div> <p> Situs ini dalam bahasa lain: <a href="/web/20240802231934/https://www.ruby-lang.org/bg/">Български</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/de/">Deutsch</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/en/">English</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/es/">Español</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/fr/">Français</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/id/">Bahasa Indonesia</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/it/">Italiano</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/ja/">日本語</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/ko/">한국어</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/pl/">polski</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/pt/">Português</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/ru/">Русский</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/tr/">Türkçe</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/vi/">Tiếng Việt</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/zh_cn/">简体中文</a>, <a href="/web/20240802231934/https://www.ruby-lang.org/zh_tw/">繁體中文</a>. </p> <p><a href="/web/20240802231934/https://www.ruby-lang.org/id/about/website/">Situs ini</a> dengan bangga dirawat oleh anggota komunitas Ruby.</p> </div> </div> </body> </html> <!-- FILE ARCHIVED ON 23:19:34 Aug 02, 2024 AND RETRIEVED FROM THE INTERNET ARCHIVE ON 19:32:46 Nov 27, 2024. JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE. ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C. SECTION 108(a)(3)). --> <!-- playback timings (ms): captures_list: 0.593 exclusion.robots: 0.027 exclusion.robots.policy: 0.017 esindex: 0.012 cdx.remote: 11.156 LoadShardBlock: 330.295 (3) PetaboxLoader3.datanode: 166.862 (4) PetaboxLoader3.resolve: 145.945 (2) load_resource: 156.569 -->