Skip to content

Instantly share code, notes, and snippets.

@jmadden
Last active June 3, 2016 00:58
Show Gist options
  • Save jmadden/9037095 to your computer and use it in GitHub Desktop.
Save jmadden/9037095 to your computer and use it in GitHub Desktop.

Ruby Basics – A Quick Reference Guide

Introduction

This document is meant to server as a jumping off point and reference guide for anyone that is new to programming in Ruby. This document will have some very basic concepts and may be useful for anyone needing a quick jog of the memory when first branching out(Git pun intended) into the brave new world of developing with Ruby. This document is not a complete review of Ruby and everything the language offers.

NOTE: If you are using this document you should have a rudimentary understanding of how to use terminal/commandline, irb and how to install Ruby Gems.

Working with variables

String interpolation vs. concatenation

In the following example we declare an age variable and then print it out to the screen in two different ways. The first way uses string interpolation, the second uses string concatenation.

age = 40
puts "my age is #{age}"
puts "my age is" + age.to_s

The age variable is an integer and when we use string interpolation (this part: #{age}) we are in essence turning the age integer into a string. Ruby automatically calls the to_s method on that variable to convert it into a string. While using string concatenation we have to manually add the to_s method to convert the age to a string.

Using variable types correctly

If you have variables that are numbers represented as strings, you will need to convert them to an integer or float to perform math operations. For example:

num1 = '5'
num2 = '7'
result = num1 + num2

This example is going to concatenate the string and return the variable result with the value of a string "57". We will need to convert the variables to integers using to_i in order to perform addition. For example:

num1 = '5'
num2 = '7'
result = num1.to_i + num2.to_i

Now the result variable will have the integer value of 12.

Variable scope

Local variables declared or initialized on an outer scope is available to an inner scope. A variable created at an inner scope is not available at an outer scope. A do...end or {...} block is considered a scope. Variables declared in one of those scopes is not available to code outside of those scopes.

Pass by reference and pass by value

Ruby is pass by value of the reference. A simpler way to think of it is whether or not there is a method that mutates the caller. If there is than the outer object was modified if not then it was not modified.

Pass by reference example. i.e. method mutates the caller.

def some_method(do)
  obj.uniq!
end

outer_obj = [1, 2, 2, 3, 3]
some_method(outer_obj)

puts outer_obj

Above Outputs: [1, 2, 3]

Pass by value example. i.e. method does not mutate the caller.

def some_method(do)
  obj.uniq
end

outer_obj = [1, 2, 2, 3, 3]
some_method(outer_obj)

puts outer_obj

Above Outputs: [1, 2, 2, 3, 3]

Debugging basics

Using the Ruby Gem Pry to do basic debugging

when using Pry to debug a Ruby program make sure you have require pry at the top of the .rb file you want to debug. In the same file you would then put binding.pry in sections of your code you'd like to examine when you run your code. When you run your application it will stop execution of your application at the point you inserted the binding.pry. Example:

You start your application by running it in terminal, ruby your_app_name.rb. The code executes and when it hits the binding.pry section of your code is produces the following:

17: num2 = gets.chomp
18: say "1) add 2) subtract 3) multiply 4) divide"
19: operator = gets.chomp
20:
21: binding.pry
22:
23: if operator == '1'
24:   result = num1.to_i + num2.to_i
25: elsif operator == '2'
26:   result = num1.to_i - num2.to_i

[1] pry(main)>

Pry will stop execution of the programon the line in which binding.pry was placed. We can now review the state of our application and look into things like the applications variables by doing the following:

[1] pry(main)> num1
=> "5"
[2] pry(main)> num2
=> "7"
[3] pry(main)> operator
=> "1"
[4] pry(main)> result
=> nil

Once finished reviewing the code, press control+d to continue execution of the application.

We can even change the value of a variable while pry is active, then allow the application to continue execution and see how that variable change affected the application.

More coming soon...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment