Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
How was last week's homework? Do you have any questions or concepts that you'd like to discuss?
# Java
for(int i = 0; i < 5; i++)
# Ruby
5.times do
Try out some calculator functions
3 + 4
2 * 4
6 - 2
4 / 2
4 /* 2
=> SyntaxError: (irb):10:syntax error, unexpected *
irb> hi
NameError: undefined local variable or method `hi' for main:Object
# Everything after this pound 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
# create and assign in one line
age = 0
# access the value in age
age
# replace value in age
age = 40
# good
occupation = "software engineer"
# bad
occupation_i_always_dreamed_of_having = "software engineer"
o = "software engineer"
1st_occupation = "software engineer"
# error
name
name = "Melissa"
defined?(name)
Built-in: Numbers, Strings, Symbols, Booleans, Regular Expressions, Arrays, Ranges & Hashes
irb> 1.class
=> Fixnum
irb> "Hello".class
=> String
irb> 1.0022.class
=> Float
irb> [1,2,3].class
=> Array
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 by 4 concatenates (links) four strings together
=>"Spam Spam Spam Spam "
"Hello".upcase
"Hello".downcase
"hello".capitalize
"hello".reverse
"hello".length
"hello"[1]
"Hello everyone".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?
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
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
for 175,200 hours?
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
"red"
# permanent and immutable
:red
This is another Object that we will learn about later.
For now, when you hear Boolean, think TRUE and FALSE.
# true
true
=> true
# NOT true
!true
=> false
# false
false
=> false
# NOT false
!false
=> true
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.",
"carrot" => "A vegetable."
}
irb> puts "hello"
hello
=> nil
irb> "2".to_i
=> 2
irb> 2.to_s
=> "2"
irb> "2".apple
NoMethodError: undefined method `apple' for "2":String
Make a file called name.rb
# navigate to your hard drive
cd ~/ #Linux, Mac
cd C:\ #Windows
# navigate into the directory we made last week
cd code
# make and navigate into a new directory for this class
mkdir gdi_class1
cd gdi_class1
# create a file
touch name.rb
Put the code below in your file called name.rb and save it
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 + '. :)'
Run your program from the command line:
ruby name.rb
# 1 dog year = 7 human years
user_age.to_i
# YOU ARE 5 YEARS OLD IN DOG YEARS
# YOU ARE 5 YEARS OLD IN DOG YEARS (32 characters)
Review: Read sections 1-5 of Ruby in 100 Minutes. Read about appreciating error messages.
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- Write in prose how you'd break up the Deaf Grandma program into small pieces. We'll discuss that exercise in class but don't try to code any of the exercises yet, though.