5. def
why
do
i
need
to
learn
it?
ruby
on
rails
=
'Ruby
language'
+
'Rails
framework'
read
the
slides!
unless
u
likez
jsp
end
6. def
what
makes
ruby
=
:special
features
=
['Object-‐oriented']
features
<<
'Interpreted'
features
<<
'Open-‐source'
#
And
features
<<
'Mixins'
#
somewhat
similar
to
multiple
inheritance
features
<<
"Native
threads"
features
<<
"Large
standard
library"
features
<<
"Centralized
package
management
through
RubyGems"
features
<<
"Built-‐in
support
for
rational
and
complex
numbers"
features
<<
"Automatic
garbage
collection"
features
<<
"Default
arguments"
features
<<
"Literal
notation
for
arrays,
hashes,
regex
and
symbols"
features
<<
"Dynamic
typing
and
Duck
typing"
features
<<
"Operator
overloading"
features
<<
"Flexible
syntax
that
serves
as
a
foundation
for
DSL"
end
7. Ruby originated in Japan during the mid-1990s
Created by Yukihiro ‘Matz’ Matsumoto
15. Types in Ruby
%q{Text}
#
Single
quoted
%Q{Text}
#
Double
quoted
mystring
=
<<LOREM
Donec
id
elit
non
mi
porta
gravida
at
eget
metus.
Morbi
leo
risus,
porta
ac
consectetur
ac,
vestibulum
at
eros.
Curabitur
blandit
tempus
porttitor.
Vestibulum
id
ligula
porta
felis
euismod
semper.
Etiam
porta
sem
malesuada
magna
mollis
euismod.
Maecenas
sed
diam
eget
risus
varius
blandit
sit!
LOREM
22. Types in Ruby
Alternate notation when
keys are symbols
roles
=
{
:LA
=>
'Gaspar',
:PM
=>
'Tavares'
}
roles
=
{
LA:
'Gaspar',
PM:
'Tavares'
}
RUBY
1.9.2
23. Operators
==
!=
<
>
<=
>=
<=>
===
.eql?
Combined comparison
0
if first equals second
1
if first is greater than the second
.equal?
-‐1
if first operand is less than the second
24. Operators
==
!=
<
>
<=
>=
<=>
===
True if both have the
.eql?
same type and values
.equal?
1.0
==
1
#
true
1.0.eql?
1
#
false
25. Operators
==
!=
<
>
<=
>=
<=>
===
.eql?
True if both have
the same object id
.equal?
a
=
1
b
=
1
a.equal?
b
#
true
(why?)
26. Operators
==
!=
<
>
<=
>=
<=>
===
.eql?
x
?
y
:
z
.equal?
Ternary operator
31. Variables
banana
#
is
a
variable
Banana
#
is
a
constant
@banana
#
is
an
instance
variable
@@banana
#
is
a
class
variable
$banana
#
is
a
global
variable
32. Variables
banana
#
is
a
variable
Banana
#
is
a
constant
@banana
#
is
an
instance
variable
@@banana
#
is
a
class
variable
$banana
#
is
a
global
variable
35. Modules
- You shouldn’t put things in a class that don’t
really go together
- Modules are a good excuse to break that rule
without breaking it
And because…
43. Flow control
if
something
puts
"It's
so
true."
elsif
another_thing
puts
"Yeah!"
else
puts
"Wrong!"
end
puts
'Hello!'
if
something
redirect_to(home_path)
unless
current_user.logged_out?
44. Flow control
case
http_status_code
case
when
404:
puts
"Not
found"
when
http_status_code
<
200
when
400
puts
"Informational"
when
http_status_code.between?(200,
300)
puts
"Bad
Request"
puts
"Success!"
else
else
puts
"Another
status
code"
puts
"Maybe
next
time."
end
end
57. Create a class with a method which prints
5 times the sentence “Hello World!”
0
class
Sample
1
def
hello
2
5.times
{
print
"Hello
World!"
}
3
end
4
end
5
6
sample
=
Sample.new
7 sample.hello
58. Create a method which reads a text file and
prints the number of characters, words and lines
0
lines
=
File.new('file').readlines
1
puts
"Chars=%d,
Words=%d,
Lines=%d"
%
[lines.join.size,
lines.join.split.size,
lines.size]
59. Use Ruby to make an HTTP GET request on
www.ruby-lang.org/en/LICENSE.txt
0
require
'net/http'
1
Net::HTTP.start('www.ruby-‐lang.org',
80)
do
|http|
2
print
http.get('/en/LICENSE.txt').body
3
end
61. 1 A Quick (and Hopefully Painless) Ride Through Ruby, viewed 2011/03/31, http://
mislav.uniqpath.com/poignant-guide/book/chapter-3.html
2 Using Ruby - An Introduction to Ruby for Java Programmers, viewed 2011/03/31,
http://onestepback.org/articles/usingruby/
3 Ruby Operators, viewed 2011/03/31, http://www.tutorialspoint.com/ruby/
ruby_operators.htm
4 Mr. Neighborly's Humble Little Ruby Book, viewed 2011/03/31,
http://humblelittlerubybook.com/
5 Ruby for Java programmers, viewed 2011/03/31, http://www.softwaresummit.com/
2006/speakers/BowlerRubyForJavaProgrammers.pdf
* splash@dei.uc.pt 31
March
2011
62. http://ruby.runpaint.org/ (draft of a book about Ruby 1.9)
http://humblelittlerubybook.com/ (book about Ruby 1.8)
http://mislav.uniqpath.com/poignant-guide/ (guide featuring talking foxes)
http://www.rubyist.net/~slagell/ruby/ (another guide)
* splash@dei.uc.pt 31
March
2011