Intro to Ruby

Class 1

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important.
  • Help each other.
  • Have fun!

Welcome!

Tell us about yourself.

  • Who are you?
  • What do you hope to get out of this class?

What we will cover today

  • Installing Ruby & the tools you'll need
  • What is programming?
  • Why Ruby?
  • Variables, Arithmetic, and Objects

A little help from our friends...

We'll be using Railsbridge's Installfest instructions to get set up. Railsbridge is an awesome organization that we're friends with and you should be, too!

Installfest

Here it is!

http://docs.railsbridge.org/installfest/

Just work through the instructions for your operating system. Stop after the "RVM OSX"/second page for Mac or the first page for Windows or Linux. Ask a volunteer for help when you get stuck or lost. This process should take about 45 minutes.

What is programming?

  • Teaching the computer to do a task.
  • A program is made of one or more files of code, each of which solve part of the overall task.
  • Programming code is human readable but also needs a form that the computer can run directly. This form is not human readable.

Why Ruby?

  • It reads like English. The syntax is intuitive.
  •               
    # Java
    for(int i = 0; i < 5; i++)
    
    # Ruby
    5.times do
                  
                
  • The community is large, active and interested in helping others learn.
  • You can get a web app off the ground quickly.
  • You can make a lucrative career out of it.
  • Using Ruby will cost you $0.

What is Ruby used for?

  • Web development (Rails)
  • iPhone apps (RubyMotion)
  • System Administration (Chef)
  • Testing (Vagrant)
  • Security (Metasploit)

Who is using Ruby?

Working with IRB

  • IRB is a command line interface to ruby that is installed when we install Ruby. It works like a calculator and is a great way to get started quickly.
  • Open: type `irb` in Terminal or Command Prompt with Ruby and Rails
  • Quit: Type `quit` and then press `enter`
  • Follow along with the examples in the slides. Type them in!
  • Feel free to explore as well. You will not accidentally break things.

Arithmetic

Try out some calculator functions

            
3 + 4
2 * 4
6 - 2
4 / 2
4 /* 2
=> SyntaxError: (irb):10:syntax error, unexpected *
            
          

Errors are Neat!

  • There are different kinds of errors that can occur. We just saw the SyntaxError which often helps us find misspelled words or missing quotes.
  • Take time to read each Error. They often tell you how to fix the issue and can be a great starting point when searching for help online.
            
irb> hi
NameError: undefined local variable or method `hi' for main:Object
            
          

Variables

  • Variables are references to information.
  • We reference variables by name.
  • They are called variable because the information they reference may change.

# Everything after this pound sign will not be evaluated.
# These are comments and I will use them to annotate code
age = 50
days_in_year = 365
days_alive = age * days_in_year
=> 18250
          

What can I do with a Variable?

  • Create - Initialize
  • Access - Read
  • Assign - Replace

# create and assign in one line
age = 0

# access the value in age
age

# replace value in age
age = 40
          

Variable naming

  • All lower case, letters and numbers only, start with letter
  • Express what is stored inside
  • Should be understood by someone who has no idea what your program does
  • Should be unique

# good
occupation = "software engineer"

# bad
occupation_i_always_dreamed_of_having_in_seattle = "software engineer"
o = "software engineer"
1st_occupation = "software engineer"
          

Data Types

Built-in: Numbers, Strings, Symbols, Booleans, Regular Expressions, Arrays, Ranges & Hashes

  • Data always has a "type"
  • You will hear the words Class/Type/Object used interchangeably
  • Let's explore a few built in Objects

irb> 1.class
=> Fixnum
irb> "Hello".class
=> String
irb> 1.0022.class
=> Float
irb> [1,2,3].class
=> Array
          

Strings

Strings are characters inside double or single quotes.


a = 'Hello '
b = "World"
c = a + b
c.reverse
=> "dlroW olleH"
          

a = "Spam "
b = a * 4
# Here multiplying a "Spam " by 4 concatenates (links) four strings
=>"Spam Spam Spam Spam "
together
          

String Practice


"Heather".upcase
"Heather".downcase
"heather".capitalize
"Hello".reverse
"Heather".length
"Heather Moore".swapcase
"".empty?
          

What is the reverse of your name?

How many characters long is your name?

Can you repeat the word hello 100 times?

What is the 5th character of my name?

Numbers

Numeric data comes in two types: Integers and Floats

Integers are either Fixnum or Bignum. They do not have decimals.


irb> 1_000_000_000_000_000_000.class
 => Fixnum
irb> 1_000_000_000_000_000_000_000.class
 => Bignum
          

Floats are numbers with at least one number to the left of a decimal point.


            irb> 1.001.class
             => Float
          

Number Practice


7/8
7.0/8.0
3.14.to_s
3.14.to_i
1 + "2"
1 + "2".to_i
(1 + 2) * 3
1 + (2 * 3)
          

How many seconds are in an hour?

How many minutes are in a week?

How many years old are you if you've been alive 1 million seconds?

More...

There are other data types in Ruby, but we'll get to them in future weeks.

To learn more about what methods are available to different classes, check out the documentation! http://ruby-doc.org/core-2.2.1/

Resources

Learning is more fun with people!

  • Start a study group with some classmates
  • Visit a Rails Meetup

Homework

  • Read Chapters 3 and 4 in Learn to Program, do as many excercises as you like.
  • If you don't have previous programming experience, or would like a review of what we did today, also read Chapters 1 and 2.
  • Bonus: Start on Code Academy's Ruby track.