Dreaming of Code

Ruby Basics

May 10, 2015

Here's some reference material about ruby basics.

Arrays and Hashes

array = [1,2,3,4,5] # => [1,2,3,4,5]
array.pop # => 5
array # => [1,2,3,4] The array has been modified by the pop method
hash = {key1: 'value1',key2: 'value2',key3: 'value3'} # => {:key1=>"value1", :key2=>"value2", :key3=>"value3"}
hash.keys # => [:key1, :key2, :key3]
hash.values # => ["value1", "value2", "value3"]

Conditional Statements

time = Time.now
hour = time.hour
# a conditional that tells youn what time of day it is
if hour > 17
  puts "Evening"
elsif hour > 12
  puts "Afternoon"
  puts "Morning"

Iterators and Loops

array = [1, 2, 3, 4, 5]
new_array = []
array.each do |val|
  new_array << val # adds val to new_array
new_array # => [1, 2, 3, 4, 5]

# loops through ten times and outputs the numbers 1 through 10
i = 1
while i <= 10
  puts i
  i += 1  

# does the same as above
i = 1
10.times do
  puts i
  i += 1


# defines a new method that outputs "Hello World!"
def hello_world
  puts "Hello World!"
hello_world # outputs "Hello World!"

# method that takes a parameter
def say(message)
  puts message
say("Hello World!") # outputs "Hello World!"

Object Oriented Programming

class Furniture
  attr_accessor :materials # allows for modification

  def initialize
    @materials = []

  def pick_materials(material)
    self.materials << material

  def print_materials
    p materials

new_furniture = Furniture.new # instantiates new object from Furniture class
new_furniture.print_materials # => ["wood", "metal", "fabric"]

Using Sinatra to Create a Web App with Ruby

Sinatra is a framework that uses Ruby to create simple quick web applications. Learn more about sinatra here: http://www.sinatrarb.com

# use get method for rendering content
get '/about' do
  "This is a page about nothing"

# use an erb template to render content
get '/about' do
  erb :about

In the views directory create a file called about.erb with the following:

<p>This page is about nothing</p>

Post method to get variables from a submitted form

post '/post' do
  form_variable = params[:form_variable]

Adding AJAX

Let's say we have an html form that we would like submit and replace some html on the page without refreshing the page. We can do this with ajax. Let's say our about.erb template part is something like the following:

<div id="about">
  <p>Here's a page about nothing</p>
  <form method="post" action="">
    <input type="submit" id="submit" value="Submit">

Since it's a page about nothing, let's make another template part called about_new.erb with the following:

<div id="about">
  <p>Here's a page about something</p>

Our sinatra methods are created below. The post method is used on about-new route so that it can handle the ajax request.

get '/about' do
  erb :about

post '/about-new' do
  erb :about_new

Finally, create a javascript file to perform the ajax request when the submit button is clicked.

$(document).ready(function() {
  $(document).on("click", "#submit", function() {
      type: 'POST',
      url: '/about-new'
    return false;

Also, make sure to load jquery and the javascript file like so:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/application.js"></script>

And that's how to use AJAX in combination with sinatra.