Skip to content

Instantly share code, notes, and snippets.

@jvidalba1
Last active August 29, 2015 14:09
Show Gist options
  • Save jvidalba1/9a65ca4dad914a4ca6a9 to your computer and use it in GitHub Desktop.
Save jvidalba1/9a65ca4dad914a4ca6a9 to your computer and use it in GitHub Desktop.
Ruby brief

Introduction to Ruby

Things about Ruby

  • Everything in Ruby is an Object (except blocks) and everything has methods.
  • In Ruby, {} are generally interchangeable with the keywords do and end.
  • There is no method overloading, the last one declared is invoked, because it overrides the others.
  • Instance variables (attributes) are trapped inside the instance and need to be published throught attribute accessors methods, for instance.

Comments

# Sigle line

=begin
  Multi
  line
  comment
=end

Naming conventions

  • Ruby is case sensitive.
  • Local variables are snake_cased.

Methods that end with !

Ruby has many methods that end with !. Whenever you see !, think: "This method may be dangerous and do something unexpected!" In many cases, the unexpected part is that the original variable gets changed, not a copy. In others cases, it's used when we're going to change something in the database.

Methods that end with ?

As a general rule, Ruby methods that end with ? evaluate to the boolean values true or false

Variables and data types

Math

  • Exponentiation: **

print, puts and p

  • print takes whatever you give it and prints it to the screen without a new line.
  • puts prints to the screen and adds a newline. Invokes the to_s method of the receiver.
  • p invokes the inspect method of the receiver.
# These two lines are equivalent:
p an_object
puts an_object.inspect

gets and gets.chomp

gets gets input from the user. Ruby adds a new blank line after each bit of input; gets.chomp removes that extra line. Your program will work fine without chomp, but you'll get extra blank lines everywhere.

first_name = gets.chomp

String methods

Ruby doc

  • size

'Ruby'.size => 5

  • reverse

'Ruby'.reverse => "ybuR"

  • upcase

'Ruby'.upcase => "RUBY"

  • downcase

'Ruby'.downcase => "ruby"

  • capitalize

'ruby'.capitalize => "Ruby"

  • include?
'Ruby'.include? 'R' => true
'Ruby'.include? 'r' => false
  • gsub

'Ruvy'.gsub /v/, 'b' => "Ruby"

  • split
greeting = 'Hello, How you doing?'
greeting.split ' '
=> ["Hello,", "How", "you", "doing?"]

String interpolation

Requires double quotes.

name = 'Mateo'
"Your name is #{name}" 
=> "Your name is Mateo"

Control flow

if, elesif and else

if x < y
  'x is less than y'
elsif x > y
  'x is greater than y'
else
  'x is equals y'
end

unless

unless hungry
  'Time to write Ruby programs'
else
  'Time to eat'
end

Loops and iterators

while

n = 1
while n <= 10
  puts n
  n += 1
end

until

n = 1
until n > 10
  puts n
  n += 1
end

for

for n in 1..10
  puts n
end

loop iterator

An iterator is Ruby method that repeately invokes a block of code

i = 10
loop do
  i -= 1
  puts i
  break if i <= 0
end

The next keyword skips over certain steps in the loop. For instance, if we don't want to print out the even numbers:

i = 10
loop do
  i -= 1
  next if i % 2 == 0
  puts i
  break if i <= 0
end

Each iterator

array = [1, 2, 3, 4, 5]
array.each do |e|
  puts e
end

Times iterator

10.times do
  puts 'Mateo'
end

upto and downto iterator

3.upto(6) do |n|
  puts n
end

10.downto(1) do |n|
  puts n
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment