background preloader

Ruby Basic Tutorial

Ruby Basic Tutorial
Troubleshooters.Com, Code Corner and Ruby Revival Present Ruby Basic Tutorial Copyright (C) 2005 by Steve Litt Note: All materials in Ruby Revival are provided AS IS. By reading the materials in Ruby Revival you are agreeing to assume all risks involved in the use of the materials, and you are agreeing to absolve the authors, owners, and anyone else involved with Python Patrol of any responsibility for the outcome of any use of these materials, even in the case of errors and/or omissions in the materials. If you do not agree to this, you must not read these materials. To the 99.9% of you honest readers who take responsibility for your own actions, I'm truly sorry it is necessary to subject all readers to the above disclaimer. CONTENTS This is a Ruby tutorial for one not knowing Ruby. Ruby can be used as a fully object oriented language, in which case you'd create classes and objects to accomplish everything. This is the simplest possible Ruby program, hello.rb. Let's count to 10... Retry Related:  Ruby Coding

Introduction to Ruby Programming - Online Course Learn how to create an interactive Web blog that allows visitors to register and post articles and comments. You'll see how to use the Ruby on Rails framework environment to create a full-featured Web blog using the Ruby programming language and the MySQL database server. We'll walk through the development of a complete Web blog application. During the course, you'll see step-by-step how to create all of the software and database objects used in the application. We'll begin by looking at exactly what the Ruby programming language is. Ruby has taken the Web world by storm, providing an object-oriented approach to Web programming. After going through the basics of Ruby programming, we'll turn to the Ruby on Rails framework environment.

Module: Open3 (Ruby 2.0) Open stdin, stdout, and stderr streams and start external executable. In addition, a thread for waiting the started process is noticed. The thread has a pid method and thread variable :pid which is the pid of the started process. Block form: Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr| pid = wait_thr.pid ... exit_status = wait_thr.value } Non-block form: stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts]) pid = wait_thr[:pid] # pid of the started process. ... stdin.close # stdin, stdout and stderr should be closed explicitly in this form. stdout.close stderr.close exit_status = wait_thr.value # Process::Status object returned. The parameters cmd... is passed to Process.spawn. Open3.popen3("echo abc") {|i, o, e, t| ... } Open3.popen3("echo", "abc") {|i, o, e, t| ... } Open3.popen3(["echo", "argv0"], "abc") {|i, o, e, t| ... } If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn.

Ruby in Twenty Minutes Introduction This is a small Ruby tutorial that should take no more than 20 minutes to complete. It makes the assumption that you already have Ruby installed. (If you do not have Ruby on your computer install it before you get started.) Interactive Ruby Ruby comes with a program that will show the results of any Ruby statements you feed it. Open up IRB (which stands for Interactive Ruby). If you’re using macOS open up Terminal and type irb, then hit enter. irb(main):001:0> Ok, so it’s open. Type this: "Hello World" irb(main):001:0> "Hello World" => "Hello World" Ruby Obeyed You! What just happened? irb(main):002:0> puts "Hello World" Hello World => nil puts is the basic command to print something out in Ruby. Your Free Calculator is Here Already, we have enough to use IRB as a basic calculator: irb(main):003:0> 3+2 => 5 Three plus two. irb(main):004:0> 3*2 => 6 Next, let’s try three squared: irb(main):005:0> 3**2 => 9 In Ruby ** is the way you say “to the power of”. Ok, wait, what was that last one?

Ruby in Twenty Minutes What if we want to say “Hello” a lot without getting our fingers all tired? We need to define a method! irb(main):010:0> def hi irb(main):011:1> puts "Hello World!" irb(main):012:1> end => :hi The code def hi starts the definition of the method. The Brief, Repetitive Lives of a Method Now let’s try running that method a few times: irb(main):013:0> hi Hello World! Well, that was easy. What if we want to say hello to one person, and not the whole world? irb(main):015:0> def hi(name) irb(main):016:1> puts "Hello #{name}!" So it works… but let’s take a second to see what’s going on here. Holding Spots in a String What’s the #{name} bit? irb(main):019:0> def hi(name = "World") irb(main):020:1> puts "Hello #{name.capitalize}!" A couple of other tricks to spot here. Evolving Into a Greeter What if we want a real greeter around, one that remembers your name and welcomes you and treats you always with respect. The new keyword here is class. So how do we get this Greeter class set in motion?

Ruby Variables, Constants and Literals Variables are the memory locations which hold any data to be used by any program. There are five types of variables supported by Ruby. You already have gone through a small description of these variables in previous chapter as well. These five types of variables are explained in this chapter. Ruby Global Variables: Global variables begin with $. Assignment to global variables alters global status. Here is an example showing usage of global variable. #! Here $global_variable is a global variable. NOTE: In Ruby you CAN access value of any variable or constant by putting a hash (#) character just before that variable or constant. Global variable in Class1 is 10Global variable in Class2 is 10 Ruby Instance Variables: Instance variables begin with @. Here is an example showing usage of Instance Variables. #! Here, @cust_id, @cust_name and @cust_addr are instance variables. Ruby Class Variables: Class variables begin with @@ and must be initialized before they can be used in method definitions. #! #! #! #!

Documentation Here you will find pointers to manuals, tutorials and references that will come in handy when you feel like coding in Ruby. Installing Ruby Unless you only want to try Ruby in the browser (see the links below) you need to have Ruby installed on your computer. You can check whether Ruby already is available by opening a terminal and typing This should output some information on the installed Ruby version. Getting Started Try Ruby! An interactive tutorial that lets you try out Ruby right in your browser. Ruby Koans The Koans walk you along the path to enlightenment in order to learn Ruby. RubyMonk Discover Ruby idioms, learn lessons and solve problems, all in your browser! Hackety Hack The little coder’s starter kit. Why’s (Poignant) Guide to Ruby An unconventional but interesting book that will teach you Ruby through stories, wit, and comics. Ruby in Twenty Minutes A nice tutorial covering the basics of Ruby. Ruby from Other Languages Coming to Ruby from another language? Learning Ruby Manuals

5 ways to run commands from Ruby | mentalized Every so often I have the need to execute a command line application from a Ruby application/script. And every single time I fail to remember what the different command-executing methods Ruby provides us with do. This post is primarily a brain dump to aid my failing memory, and it was triggered by an issue with my Redmine Github Hook plugin where STDERR messages were not being logged. The goal of this exercise is basically to figure out how to run a command and capture all its output – both STDOUT and STDERR – so that the output can be used in the calling script. err.rb The test script I’ll be running basically outputs two lines, one on STDOUT, the other on STDERR: #! Kernel#` (backticks) Returns the standard output of running cmd in a subshell. >> `. STDERR is output, but not captured STDOUT is captured Kernel#exec Replaces the current process by running the given external command. >> exec('. Both STDERR and STDOUT is output. Kernel#system >> system('. IO#popen >> output = IO.popen('. Open3#popen3

Ruby Tutorial with Code Samples Ruby, Ruby on Rails, and _why: The disappearance of one of the world’s most beloved computer programmers Illustration by Charlie Powell. In March 2009, Golan Levin, the director of Carnegie Mellon University’s interdisciplinary STUDIO for Creative Inquiry, invited an enigmatic and famed computer programmer known to the virtual world only as “Why the Lucky Stiff” or “_why”—no, not a typo—to speak at a CMU conference called Art && Code—also not a typo—an event where artsy nerds and nerdy artists gather to talk shop. _why came to Pittsburgh and presented his latest project to a room full of a student programmers and artists. He was scruffily handsome, seemingly in his early- to mid-30s, with shaggy brown hair falling in his eyes and a constant half-smile. At this symposium, he wore a pair of oversize sunglasses and a tidy sports coat with a red pocket square, a silly riff on a stuffy professor’s outfit. He riffed on his nom d’Internet, Why the Lucky Stiff: “Some people want to call me Mr. Back in the old days, you could hack your Commodore 64 without too much trouble. Why? That’s it.

Ruby Quiz Ruby Programming Ruby is an interpreted, object-oriented programming language. Its creator, Yukihiro Matsumoto, a.k.a “Matz,” released it to the public in 1995. Its history is covered here. Its many features are listed here. The book is currently broken down into several sections and is intended to be read sequentially. Table of Contents[edit] Getting started[edit] Overview Installing Ruby Ruby editors Notation conventions Interactive Ruby Mailing List FAQ Basic Ruby[edit] Hello world Strings Alternate quotes Here documents Encoding Introduction to objects Ruby basics Data types — numbers, strings, hashes and arrays Writing methods Classes and objects Exceptions Ruby Semantic reference[edit] See also some rdoc documentation on the various keywords. Built in Classes[edit] This is a list of classes that are available to you by default in Ruby. Available Standard Library Modules[edit] These are parts of Ruby that you have available (in the standard library, or via installation as a gem). Other Libraries[edit] GUI Libraries[edit]

Related: