!SLIDE subsection
This section is intended as a brief, lightweight overview of the Ruby language; following sections will cover all these topics in much more detail. Students are encouraged to ask questions, but instructors are encouraged to answer, "We'll cover that later."
Q: Did you have a guiding philosophy when designing Ruby?
A: Yes, it's called the "principle of least surprise."
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
- Ruby has a humane interface
- many ways to do things
- Ruby favors readability and variety over concision and perfection
- sometimes makes code hard to understand (but usually makes it easier)
- contrast to minimal interface
- one (or a few) "right" ways to do things
- Python has a minimal philosophy
- Ruby 1.0 released in 1996
- Fully Open Source
- MRI 1.8.7
- MRI 1.9.2 or 1.9.3
- JRuby
- Interpreted
- Dynamically typed
- Object oriented
- Blocks / lambdas / closures
- Perl-like regular expressions
- Closely tied to shell & OS
@@@ ruby
$ irb
>> 4
=> 4
>> 4+4
=> 8
Please fire up irb
on your computer and try this out right now!
@@@ ruby
>> 2 + 2
=> 4
>> (2+2).zero?
=> false
>> "foo" if true
=> "foo"
>> "foo" if false
=> nil
>> puts "foo"
foo
=> nil
These are equivalent:
@@@ ruby
def inc(x)
return x + 1;
end
def inc x
x + 1
end
def inc(x); x + 1; end
def inc(x) x + 1; end
@@@ ruby
x = 1 + 2
x #=> 3
x = 1
+ 2
x #=> 1
Solution: always put operators on top line
x = 1 +
2
x #=> 3
@@@ ruby
>> "Hello".gsub "H", "h"
=> "hello"
>> "Hello".gsub "H", "h".reverse
=> "hello"
>> "Hello".gsub("H", "h").reverse
=> "olleh"
@@@ ruby
# is a comment
2 + 2 # is a comment
Ruby has a syntax for multiline comments too, but it's silly and nobody uses it.
!SLIDE
@@@ ruby
first_name = "Santa"
last_name = "Claus"
full_name = first_name + last_name
#=> "SantaClaus"
!SLIDE custom
- Numbers
42
(Fixnum)3.14159
(Float)
- Booleans
true
false
- Strings
"apple"
'banana'
- Symbols
:apple
- Regular Expressions
/fo*/i
- Arrays
["apple", "banana"]
- Ranges
(1..10)
- Hashes
{:apple => 'red', :banana => 'yellow'}
{apple: 'red', banana: 'yellow'}
@@@ ruby
"boyz #{1 + 1} men"
=> "boyz 2 men"
- Any Ruby code can go inside the braces
- It gets evaluated and stuck inside the string
x = 1
means "put the value1
in the variablex
"x == 2
means "true
ifx
is2
, otherwisefalse
"x === 3
means the same as==
but sometimes more- threequal is rarely used
(The Well-Grounded Rubyist, p. 5, section 1.1.2)
@@@ ruby
def add a, b
a + b
end
add 2, 2
#=> 4
- Note: no 'return' required
def add(a, b)
is also legal
Are you sick of hearing me speak?
If so, do a lab: 01_temperature is right up your alley.
@@@ ruby
class Calculator
def add(a,b)
a + b
end
end
calc = Calculator.new
calc.add(2, 2)
#=> 4
- a function inside a class is called a method
- A class defines a group of behaviors (methods)
- Every object has a class,
Object
if nothing else
- an object is referenced by a variable or a literal
- the dot operator (
.
) sends a message to an object - an object receives a message and invokes a method
- with no dot, the default object (
self
) is the receiver
- method names can end with
!
or?
?
means "boolean"!
means "watch out!"
methods and variables are in snake_case
classes and modules are in CamelCase
constants are in ALL_CAPS
Standard is better than better.
-- Anon.
local_variable
- start with letter or underscore, contain letters, numbers, underscored@instance_variable
- start with@
@@class_variable
- start with@@
$global_variable
- start with$
Constant
orCONSTANT
- must start with uppercase letterClassName
- capitalized camel casemethod_name?
- like a local variable, but can end with?
or!
or=
- keywords - about 40 reserved words (
def
) and weirdos (__FILE__
) - literals -
"hi"
for strings,[1,2]
for arrays,{:a=>1, :b=>2}
for hashes, etc.
@@@ ruby
var # local variable (or method call)
@var # instance variable
@@var # class variable
$var # global variable
VAR # constant
load
inserts a file's contents into the current filerequire
makes a feature available to the current file- skips already-loaded files
- omits the trailing
.rb
- can also be used for extensions written in C (
.so
,.dll
, etc.)
- "Ruby Intro" slides based on Ruby Quickstart for Refugees by Jacob Rothstein
- Improved by Alex Chaffee, Sarah Allen, Wolfram Arnold