Ryan Dlugosz 2016, based on GDI Seattle and Dayton courses.
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
TBD
> Ruby.easy_to_learn? and Ruby.fun_to_use?
=> true
ruby> 1 + 1
=> 2
ruby> puts "Hello World!"
Hello World!
ruby> 3.times do
puts "Hello World!"
end
Hello World!
Hello World!
Hello World!
Let's start coding for real!
For now, focus on what we're able to accomplish without worrying too much about remembering exactly how we're doing it. That intuition will develop in time!
Let's talk in more detail about some of the things we learned.
repl.it is an online REPL that supports several different languages. It's a great place to try things, and makes it easy for us to share links to our code in class.
Visit the repl.it site and select "Ruby" from the menu.
Try out some calculator functions
3 + 4
2 * 4
6 - 2
4 / 2
4 /* 2
=> SyntaxError: (irb):10:syntax error, unexpected *
Other Ruby Math Operators
# Modulo operator (returns the remainder)
11 % 5
=> 1
# Exponent operator (2 raised to the power of 3)
2 ** 3
=> 8
irb> hi
NameError: undefined local variable or method `hi' for main:Object
# Everything after the 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
like_this.symbol is a shorthand for a constant string.
# variables
age = 37
instructor_name = "Ryan Dlugosz"
# constants
BEST_FOOD = "Sushi"
PI = 3.14159
# symbols
:pizza
:ice_cream
# literals
3
'hi'
# create and assign in one line
age = 0
# access the value in age
age
# replace value in age
age = 40
# use it in a statement
age + 10 # => 50
# good
occupation = "software developer"
# bad
occupation_i_always_dreamed_of_having_in_cincy = "software developer"
o = "software developer"
1st_occupation = "software developer"
# error
name
# defined, initialize, create
name = "KC"
defined?(name) # => true
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
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 have 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 1 million seconds?
Strings are characters inside double or single quotes.
a = 'Hello '
b = "World"
c = a + b
c.reverse
=> "dlroW olleH"
a = "Spam "
# Here multiplying a by 4 concatenates (links) four strings together
b = a * 4
=>"Spam Spam Spam Spam "
"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?
irb> puts "hello"
hello
=> nil
irb> "2".to_i
=> 2
irb> 2.to_s
=> "2"
irb> "2" / 5
NoMethodError: undefined method `/' for "2":String
getsputsPut the code below in a file and save it as name.rb
puts 'Hello there, and what\'s your name?'
name = gets
puts 'Your name is ' + name.chomp! + '? What a lovely name!'
puts 'Pleased to meet you, ' + name + '. :)'
Run your program from the command line:
ruby name.rb
The following slides are included for reference, but are not required steps for completing this Intro to Ruby class. If you have any trouble, please ask me for help in the GDI Slack channel.
If you happen to be using a Mac, Ruby is already installed on your computer!
Download and run the Rails Installer for Windows (Ruby 2.2 version).
The RailsBridge organization maintains a great resource for installing Ruby on your computer. Check it out!
These instructions could be helpful if you run into trouble.
http://docs.railsbridge.org/installfest/
Just work through the instructions for your operating system. For our class, you'll only need to advance through the first steps, but you're welcome to advance further if you're curious or plan to write a Rails applications.
Working in the terminal can be really intimidating at first!
With practice, navigation and file manipulation is significantly faster in the terminal than in the GUI.
Professional software developers use the terminal all the time, and this class will require some use of it.
Mac & Linux users: Open Terminal
Windows users: Open Git-Bash
We will not be using Windows "cmd" program, as it uses a different syntax than *NIX systems.
The line that appears in the terminal when you open it is called the prompt. It usually contains information about the user and current directory. It can be customized.
Terminal instructions often start a line with a $. This represents the last character in the prompt, you don't have to type it.
. |
the current directory- ex: "touch ./wow.txt" |
.. |
the parent of current directory - ex: "cd ../RubyParty" |
~ |
the root directory- ex: "cd ~/Pictures" |
cd [location] |
"change directory" to [location] |
pwd |
"present working directory" - where am I? |
ls -al |
"list all" of the contents of current directory, including invisible ones |
touch [filename.extension] |
create a file called filename.extension in the current directory |
mkdir [directoryname] |
create a directory called directoryname in the current directory |
rm [filename] |
"remove" (delete) the file called filename |
rm -rf [directoryname] |
"remove recursively with force" the directory called directoryname |
clear OR cmd+k |
clear the terminal screen |
help |
lists all possible commands |
man [command] |
displays the manual for command |
# navigate to your desktop
cd ~/Desktop #Linux, Mac
cd desktop #Windows
# make a directory called ruby_adventures
mkdir ruby_adventures
# navigate into new directory
cd ruby_adventures
# create a file
touch class0.rb
# list the contents of the directory
ls
man ls
Output:
if/then/else and friends)Sounds like this will be great!
Wow. Can't wait to see you there!