A beginners guide to Ruby, Part 2 03-31-2015, 10:51 PM
#1
Nightc||ed's Beginner Ruby Guide
Last time, we talked about the Terminology, History and some notes on Ruby, as well as a look at the basic hello world program.
This time, I'll link some download pages and we'll get to coding!
Downloads
Ruby (core) (only one you need for this):
Source code:
http://cache.ruby-lang.org/pub/ruby/2.2/...2.1.tar.gz
Windows (RubyInstaller):
http://www.rubyinstaller.org/downloads/
OSX:
Get Linux or Windows (seriously. Do yourself a favor.)
If you must insist, follow the Linux/UNIX directions
Linux/UNIX:
you can use the package management system of your distro or some choice third-party tools (rbenv and RVM).
(see here for help.)
Rails:
We'll cover this later
Your First Program:
After installing Ruby and reading all my boring crap, you probably want to jump to code.
Here, we'll be going over a few ways to make a Hello World program. One simple, using puts; the second, still using puts, but incorporating a variable; and the third using def.
(Quick note: puts is one of the ways you can output things to the console with Ruby)
Simple:
Slightly more complex:
Kinda tricky:
The reason I didn't put sleep in the definition is, it can be called as many times as I want and the sleep class stops the script entirely. Moving on.
User Input and variable output (I/O):
Input
User input (for me, at least) is a big part of ruby and here are a few ways to get user input.
The two main ways to get user input both start with the 'gets' class.
From there, you can use the following methods:
Output:
As I previously explained, 'puts' is one of the ways to display information on the console. The others, print and p, are less relevant and tend to make your GUI look sloppy.
To put both a variable and static text in one 'puts' you need to use #{var} inside your DOUBLE quotations. Single quotations mess with it more than I care to deal with.
Example output:
For multiline puts statements you can either use
or
Thanks for reading!
Be sure to like this thread if it helped you or informed you as making these is a giant pain in the ass and I don't want to keep doing it if nobody benefits from it.
-Nightc||ed
Last time, we talked about the Terminology, History and some notes on Ruby, as well as a look at the basic hello world program.
This time, I'll link some download pages and we'll get to coding!
Downloads
Ruby (core) (only one you need for this):
Source code:
http://cache.ruby-lang.org/pub/ruby/2.2/...2.1.tar.gz
Windows (RubyInstaller):
http://www.rubyinstaller.org/downloads/
OSX:
Get Linux or Windows (seriously. Do yourself a favor.)
If you must insist, follow the Linux/UNIX directions
Linux/UNIX:
you can use the package management system of your distro or some choice third-party tools (rbenv and RVM).
(see here for help.)
Rails:
We'll cover this later
Your First Program:
After installing Ruby and reading all my boring crap, you probably want to jump to code.
Here, we'll be going over a few ways to make a Hello World program. One simple, using puts; the second, still using puts, but incorporating a variable; and the third using def.
(Quick note: puts is one of the ways you can output things to the console with Ruby)
Simple:
Code:
puts "Hello, World!"
sleep #(this is a comment, btw) sleep is the equivalent of pause or wait in other languagesSlightly more complex:
Code:
hi = "Hello, World!" #we set our variable, hi, to the value "Hello, World!"
puts hi #since hi is a variable, we don't need to use quotation marks
sleepKinda tricky:
Code:
def hi #defining hi
puts "Hello, World!"
end #ending the definition
hi
sleepUser Input and variable output (I/O):
Input
User input (for me, at least) is a big part of ruby and here are a few ways to get user input.
The two main ways to get user input both start with the 'gets' class.
From there, you can use the following methods:
Code:
gets #simple string input, but messy output
gets.chomp #same idea, but cleaner output
gets.to_s # ^
gets.to_i #get user input in the form of an integer (good practice to prompt the user for a number)Output:
As I previously explained, 'puts' is one of the ways to display information on the console. The others, print and p, are less relevant and tend to make your GUI look sloppy.
To put both a variable and static text in one 'puts' you need to use #{var} inside your DOUBLE quotations. Single quotations mess with it more than I care to deal with.
Code:
say = gets.chomp
puts "#{say}"
sleepCode:
=> hi, Ruby! #user input
hi, Ruby!For multiline puts statements you can either use
Code:
puts "hi
hi
hi"Code:
puts """
hi
hi
hi
"""Thanks for reading!
Be sure to like this thread if it helped you or informed you as making these is a giant pain in the ass and I don't want to keep doing it if nobody benefits from it.
-Nightc||ed
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.















![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)