Ryan's Manuals

Ruby

A beautifully expressive hacker language. Mostly used for rails.


Contents



Hello Ruby

puts "Hello, Ruby!"

What is Ruby?

In descriptors: high-level, object-oriented and interpreted. Ruby occupies a similar market space to Python, with both striving to create a more human-readable language. I’ll update this as I learn more, with my primary resources being Ruby Koans and Codecademy. In code blocks below, #> indicates text that is printed to the console.

Why use Ruby?

I’m not, really; it seems like a fun language with lots of respected devs. I don’t have time to learn or tinker with this very flexible scripting lang at the moment. At one point in early 2019, I spent a short, intense period binge-learning C++, then Ruby, in order to meet requirements and complete technical interviews at C++/Ruby shops.

The Basics

Running ruby -v will print your ruby version. If you have a ‘good’ OS, you should have a version built in. Upgrade if the version is less than 2. ruby runs ruby programs. irb starts an interactive ruby prompt.

On Debian, it is best to add /.gem/ruby/2.3.0/bin to your PATH in ~/.profile, and gem: --user-install to your ~/.gemrc. Be careful not to run gem, bundle, etc as root.

Adding this user bin to your PATH makes it easy to use tools like RuboCop (gem install rubocop) to rubocop --fix-layout *.rb your ruby files.

Resources:

  1. Ruby: Why’s Poignant Guide
  2. Rails: Rails Guides and railstutorial.org are good.
  3. Ruby Koans
  4. The Odin Project
  5. Writing CLI progs in Ruby

Data Types

number = 18
boolean = true
string = "Hello"

Console Output

puts "String" #Appends a newline
print "String"

puts and print are used for output, the only difference being that puts appends a newline after printing the given string.

Arithmetic

x = (3 + 3) / 2 #Brackets
x = 3 ** 2 #Exponents
x = 3 / 3 #Division
x = 3 * 3 #Multiplication
x = 3 + 3 #Addition
x = 3 - 3 #Subtraction
x = 3 % 3 #Modulo

The only operation that may be foreign is modulo, which is the remainder of a division. For instance, running 128 / 13 will give 9, and 128 % 13 will give 11. Stepping backwards, 13 * 9 is 117, and adding 11 gives 128.

Using Methods

x = "String length".length
puts x #> 13

Methods without arguments (or to be invoked with only default arguments,) can be called without brackets ( ).

puts "RoFlMaO".upcase.reverse.downcase
#> oamlfor

String Manipulation

Reverse

x = "Ruby is interesting...".reverse
puts x #> ...gnitseretni si ybuR

Upcase/Downcase

puts "Ruby is interesting...".upcase
#> RUBY IS INTERESTING...
puts "Ruby is interesting...".downcase
#> ruby is interesting...

Basic Syntax

Comments can be included anywhere, and are formatted as follows:

# Single line.

=begin
Multi-line.
=end

Names are all lowercase and words are separated with _.

large_number = 7893425089273045
player_name = "Jimothy"

Concept

Intro

puts("Hello, World!")

Section

Intro

Concept

Intro

puts("Hello, World!")

MongoDB

After installing MongoDB and loading http://media.mongodb.org/zips.json into the database, (or another from https://github.com/ozlerhakan/mongodb-json-files,)



Site Directory



Page Information

Title: Ruby
Word Count: 490 words
Reading Time: 3 minutes
Permalink:
https://manuals.ryanfleck.ca/rb/