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 is your favorite ice cream or frozen dessert?

What we will cover today

  • What is programming?
  • Why Ruby?
  • Variables, Arithmetic & Objects
  • Methods and Error Messages

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.

What is Ruby used for?

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

Who is using Ruby?

Working with a Repl

  • Repl stands for: Read, Evaluate, Print, Loop. It's an interactive programming environment where you can write and execute code. Using a Repl is great way to get started quickly, as you don't need to install anything to get started.
  • Open a browser and go to: repl.it
  • In the dropdown, choose Ruby.
  • Follow along with the examples in the slides. Type them in!

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.

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/hash sign will not be evaluated.
      # These are called comments and I will use them to annotate code
      age = 50
      days_in_year = 365
      days_alive = age * days_in_year
      => 18250
      

Ruby's Variables

  • Ruby has three kinds of variables, one kind of constant and exactly two pseudo-variables.
  • A local variable starts with a lower case letter or an underscore character and consists entirely of letters, numbers and underscores.

      age = 50
      _age = 50
      _my_age = 50
      

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

  • Express what is stored inside
  • Make it easy to read
  • 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"
      

Local Variable Values

  • No default value
  • No type declaration
  • Do not live forever

      # error
      name

      name = "Jess"
      defined?(name)
      

Data Types

  • Numbers, Strings, Symbols, Booleans, Regular Expressions, Arrays, Ranges & Hashes
  • Data always has a "type"
  • You will hear the words Class/Type/Object used interchangeably

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

Strings

Strings are characters inside double or single quotes.


      first_string = 'Hello '
      second_string = "World"
      combined_string = first_string + second_string
      combined_string.reverse
      => "dlroW olleH"
      

      name = "Jessica "
      name_multiple = name * 4
      # Here multiplying a by 4 concatenates (links) four strings together
      =>"Jessica Jessica Jessica Jessica "
      

String Practice


      name = "Jessica"
      name.upcase
      name.downcase
      name.capitalize
      name.length
      name.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 (or your name)?

Numbers

Numeric data comes in two types: Integers and Floats

Integers do not have decimals.

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


        irb> 55.class
        => Integer
        irb> 55_000.class
        => Integer
        irb> 55.001.class
         => Float
      

Number Practice


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

What is the number 1.25976 rounded to a precision of 2?

What is the the smallest possible integer that is greater than or equal to 5.2?

How would you turn the number 42 into a float? How would you turn the float 56.25 into an integer?

Symbols

You can think of symbols as lightweight Strings.

It's good to know these exist, but don't worry about them too much for now.


      # transient and mutable
      "hello"

      # permanent and immutable
      :hello
      

Booleans

This is another Object that we will learn about later.

For now, when you hear Boolean, think TRUE and FALSE.


      # NOT true
      !true
      => false

      # NOT false
      !false
      => true
      

Arrays, Ranges & Hashes

We will also leave these data types for the next class

Examples of what they will look like:


      # Arrays are used to hold sets of data.
      irb> array_of_numbers = [1,2,3]
      irb> array_of_numbers.first
      => 1

      # Ranges are used to express a sequence.
      irb> range_of_numbers = (1..5)
      irb> range_of_numbers.include?(4)
      => true

      # Hashes are like dictionaries. You can look up a value by a key.
      irb> hash_of_key_to_value =
        {
          "Apple" => "A fruit.",
          "Cucumber" => "A vegetable."
        }
      

Methods

  • Methods define the behavior for an Object.
  • String Objects can reverse, for example.
  • Some methods are accessed using our friend 'dot' and some are standalone.
        
      irb> "2".to_i
      => 2
      irb> 2.to_s
      => "2"
      irb> "2" / 5
      NoMethodError: undefined method `/' for "2":String
        
      

User Input

To obtain user input, use the `gets` keyword

To print out information, use the `puts` or `print` keyword

Let's create our first Ruby program together!


Put the code below in your repl and run it! What's happening on each line?


          puts "Hello there, and what's your name?"
          name = gets.chomp!
          puts "Your name is " + name + "? What a lovely name!"
          puts "Pleased to meet you, " + name + ". :)"
        

Let's Develop It

  • Write your own program using 'puts' and 'gets' to ask a user for their age and then tell them how old they are in dog years.
  • reminder: 'gets' method returns a string. To do math on it, convert it to an integer with `.to_i` method.
  • 
            #1 dog year = 7 human years
            user_age = gets.to_i
            

Homework

Practice: Write a command line program that asks the user for the year they were born, then calculates their age in years, days, and seconds. Tell the user how old they are in these different formats. (Note: you'll be using 'gets' and 'puts' in this program, along with some math)


Prep: Read Chapter 6 and Chapter 7 of Learn To Program- don't try to do the exercises at the end yet, though.


Install Ruby