Talk to a computer's hardware
.
The software
. It sends input
(commands
, data
, etc) to the operating system and receive output
after the operating system has acted on it.
A set of code that can be used to create an application.
Many others.
Q: How is a computer language similar to a human language, like English or Spanish? How is it different?
A collection of reusable code to accomplish a generic activity.
Collection of reusable code to facilitate development of a particular product or solution.
The rest of this tutorial isn't about Rails. You're learning how to do any kind of programming with Ruby.
I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.
-- Matz (Yukihiro Matsumoto), Ruby creator
Python, Perl, and JavaScript are scripting languages too.
Java and C++ are examples of compiled languages.
You may also hear it called "command Line", "console", "shell", or "Bash"
Windows: git bash
Mac OS X & Ubuntu: Terminal
prompt
, and customarily ends with a dollar signWhenever instructions start with "$ "
, type the rest of the line into terminal.
Let's give the terminal a command
, to open Interactive Ruby (IRB)
$ irb
IRB has its own prompt, which customarily ends with >
$ irb >
You can use Control-C
to exit IRB any time.
Or type exit
on its own line.
> exit $
Now you're back to the terminal's prompt.
Windows Users! Some people have experienced trouble with backspace, delete, and arrow keys working properly in irb - what a pain! If you run into this problem, use this command instead to launch irb. $ irb --noreadline
$ irb > my_variable = 5 => 5 > another_variable = "hi" => "hi" > my_variable = 10 => 10
What is happening on the lines beginning with =>
?
Variables are assigned using a single equals sign (=
).
The right side of the equals sign is evaluated first, then the value is assigned to the variable named on the left side of the equals.
apples = 5 bananas = 10 + 5 fruits = 2 + apples + bananas bananas = fruits - apples
What happened on each line? Is it what you expected?
What could you do to see each's return value
for confirmation?
Create a variable whose name has:
all letters (like 'folders')
all numbers like '2000'
an underscore (like first_name
)
a dash (like 'last-name')
a number anywhere (like y2k
)
a number at the start (like '101dalmatians')
a number at the end (like 'starwars2')
What did you learn?
Variables can hold many types of information, including:
Don't know what these are? Don't worry! We're about to find out!
A string is text. It must be wrapped in a matched pair of quotation marks.
$ irb > 'Single quotes work' => "Single quotes work" > "Double quotes work" => "Double quotes work" > "Start and end have to match' ">
What is happening on the last two lines? How would you solve it?
An array is a list.
Each array must be surrounded by square braces
aka square brackets
. A comma separates each member
.
> fruits = ["kiwi", "strawberry", "plum"] => ["kiwi", "strawberry", "plum"]
Members are stored in order. Each can be accessed by its index
. Ruby starts counting at zero.
> fruits[0] => "kiwi" > fruits[1] => "strawberry" > fruits[2] => "plum"
In a hash
we can refer to a member by a keyword instead of a number. Each member is a pair:
Key: address of the hash member
Value: variable contained by the member, and located by key name
A hash may also be known as a dictionary
, associative array
, or map
.
A hash is surrounded by curly braces
aka curly brackets
. A comma separates each member pair. A key uses =>
(the rocket
) to point to its value.
> states = {"CA" => "California", "DE" => "Delaware"} => {"CA"=>"California", "DE"=>"Delaware"}
In real life, what lists do we make in key/value pairs?
Member pairs can be accessed by their key. So each hash key has to be unique.
Values don't have to be unique.
> states["CA"] => "California"
A boolean is one of only two possible values: true
or false
.
> 1 + 1 == 2 => true > 1 + 1 == 0 => false
( ==
means "is equal to". More on that later.)
> my_variable + 2 => 7 > my_variable * 3 => 15
> my_fruits = fruits + ["lychee"] => ["kiwi", "strawberry", "plum", "lychee"] > my_fruits = my_fruits - ["plum"] => ["kiwi", "strawberry", "lychee"]
> fruits.each do |fruit| ?> puts fruit > end kiwi strawberry plum => ["kiwi", "strawberry", "plum"]
On the second line, what does ?>
indicate?
"I would like to visit Barcelona" "I would like to visit Antigua" "I would like to visit Alaska" "I would like to visit New Orleans"
> fruits.each do |fruit| ?> puts fruit if fruit == "plum" > end plum => ["kiwi", "strawberry", "plum"]
Ruby is an interpreted language. Its code can't run by the computer directly. It first must go through a Ruby interpreter.
The most common interpreter is Matz's Ruby Interpreter ("MRI"). There are many others.
There are various ways to run code through a Ruby interpreter. We were using IRB earlier and now we will use a file.
Note which folder your terminal is currently in, this is your working directory
In your text editor, create a file named my_program.rb
inside your working directory.
class Sample def hello puts "Hello World!" end end s = Sample.new s.hello
$ ruby my_program.rb Hello World!
$ irb > load 'my_program.rb' > second_time=Sample.new > second_time.hello
When might it be useful to do this?
hello.rb
puts "Hello, World!"
hello.rb
puts "Hello, #{ARGV.first}!"
terminal
$ ruby hello.rb Alice Hello, Alice!
hello.rb
if ARGV.empty? puts "Hello, World!" else puts "Hello, #{ARGV.first}!" end
terminal
$ ruby hello.rb Hello, World! $ ruby hello.rb Alice Hello, Alice!
Describes the generic characteristics of a single type of object.
What things of this type are.
e.g. Dog, Vehicle, Baby
Defines behavioral characteristic.
What the things of the class's type do.
e.g. Chase, Drive, Talk
Defines attribute characteristic.
What things of the class's type have.
e.g. Breed, Model Year, Favorite Ice Cream
A specific incarnation of the class.
e.g. Rin Tin Tin, garbage truck, the neighbor's kid
(start at "4. Objects, Attributes, and Methods")
Topics:
Commandline program. Practice in Ruby syntax and OOP concepts, and creating commandline programs.
Explore strings: concatenation, manipulation, interpolation, coercion.
Symbols, nil, basic math operators, blocks, method chaining, passing parameters to methods, iteration, branching, conditionals & conditional looping.
Topics:
Commandline program. Reinforce skills learned in Personal Chef.
Explore how to manipulate arrays, do more elaborate strings manipulations, refactor code, take advantage of character mapping, and access the filesystem from within code.
Topics:
Commandline program. Reusing others code & data, refactoring your own code & cleaning up data, writing custom code to solve requirements.
Gems, initialize
method, parameters, file input/output, processing/sanitizing data, looping, conditional branching, using file-based data storage (CSV, XML, JSON), accessing an external API, nils, DRY principle, constants, sort_by, more string manipulations.
A follow-up to EventManager focusing more on Ruby object decomposition and working with Command Line Interfaces and program control flow.
Event Reporter Lab Topics: Object decomposition, working with Command Line Interfaces, and program control flow. Continues project created in Event Manager lab.
/
#