3. Overview - Birthstone
January - Garnet February - Amethyst
March - Aquamarine April- Diamond
May - Emerald June - Pearl
July - Ruby August - Peridot
September - Sapphire October- Tourmaline
November - Yellow Topaz December - Blue Topaz
4. Overview
•Pure OO,Everything is Object,Yes!Everything
5.times{|i|puts i}
•Highly Dynamic
class HelloWorld;end
HelloWorld.send(:define_method,:say){puts "HelloWorld!"}
•Excute while parsing
class Klass
puts "Hello"
end
•Cross Platform
Windows,Linux,Unix,Mac,...
•Open Source
http://www.ruby-lang.org/en/downloads/
5. To C++ Programmer
•No variable declaration
•ONLY false & nil are falsy
•'constructor' is called 'initialize'
•All methods are always 'virtual'
•First letter is important: $,@,@@,Klass,i
•Member variables are private
•Use 'self' instead of 'this'
•?,! ARGV.size.zero? obj.nil? str.strip!
•Single Inheritance, Mixin to help
•Parentheses & semicolons are usually optional
•Classes are not sealed!can be reopen
•No Generic/Template
•Iteration are easy
•There's only two container: Array,Hash
•Almost everything is an expression
6. Installation & Implementations
•Windows
One-Click Installer, Zip
•Ubuntu
sudo apt-get install ruby1.8/ruby1.9
sudo apt-get install irb1.8/irb1.9
•Mac
port install ruby
•Implementations
c ruby, JRuby, IronRuby
•Version
ruby1.8 ruby1.9
8. Structure & Execution
#!/usr/bin/ruby shebang comment
# -*- coding: utf-8 -*- coding comment
require 'erb' loading library
class Klass define class
include Enumerable include module
def k
require 'socket' loading library
...
end
end
if $0 == __FILE__
o=Klass.new.k create object
DATA.read
end
__END__
embeded string embeded string resource
9. Data types - Number
dec = 123 dec.class==Fixnum
million = 1_000_000
hex = 0xFF
binary = 0b0010
octal = 0377
float = 1_000.0 float.class==Float
2**1024 =
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477
322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302
219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239
947245938479716304835356329624224137216
(2**1024).class==Bignum
Conversion:
other_type.to_i
other_type.to_f
10. Data types - Text
variable_name = %PREFIX SYM content SYM
SYM = [~`!@#$%^&*_-+=|:;"',.?/] + [] {} ()
name = 'Jack' No Escape!
name = %q[Jack]
name = %q{Jack} PREFIX = [%q,%Q,%x,%r,%s]
name = %q!Jack! Backtick command Execute
name = %q|Jack| %x[notepad] `notepad`
name = %q+Jack+
name = %q-Jack- Regular expression
name = %q=Jack= %r[hello] /hello/
name = %q,Jack,
name = %q.Jack. Array literal
name = %q/Jack/ %w[hello world] => ['hello','world']
name = %qJack %W[hello world] => ["hello","world"]
...
all symbol on Symbol literal
keyboard! %s[symbol]
13. Data types - Here Document
str = <<-STR.gsub("world","there")
hello world
hello world hello world hello world
hello world
hello world
hello world
STR
cpp_tepmlate = <<-CPPTPL
class <%=name%> : public <%=basename>
{
public:
<% methods.join("n") %>
};
CPPTPL
result = ERB.new(cpp_template).result(binding)
14. Data types - Array
As Vector:
a = [1,"h",3.14]
n = %w[1 2 3] => ['1','2','3']
e = %W[s t r n] => ["s","t","r","n"]
empty = Array.new => []
nils = Array.new(3) => [nil,nil,nil]
[0]*3 => [0,0,0]
copy = Array.new(nils) => [nil,nil,nil]
Array.new(3){|i|i} => [0,1,2]
As Set:
[1,2]+[3,4] => [1,2,3,4]
[1,2,3,4] - [1,2] => [3,4]
[1,2] | [2,3] => [1,2,3]
[1,2] & [2,3] => [2]
As Stack:
[1,3].push(3,4) => [1,2,3,4]
[1,2,3].pop => [1,2]
As Queue:
[1,2,3,4].shift => [2,3,4]
[3,4,5].unshift(2) => [2,3,4,5]
15. Data types - Array
As List:
a = [1,2]
a.concat(3) => [1,2,3]
a << 1 => [1,2,3,1]
a.insert(a.index(2),'a') => [1,'a',2,3,1]
a.delete("a") => [1,2,3,1]
a.delete(1) => [2,3]
[1,2,3,4].reject{|e|e%2==0} => [1,3]
As Sequence:
a = ('a'..'c').to_a => ['a','b','c']
a[0,1] => ['a']
a[0..2] => ['a',b','c']
a[0..-2] => ['a','b']
Iteration:
a = [1,2,3]
a.each{|e| print(e)} => 123
a.each_with_index{|e,i| print("#{i}:#{e},")} => 0:1,1:2,2:3,
a.inject(0){|s,e|s+=e} => 6
for i in a
print(i)
end => 123
17. Data types - Hash
As HashTable:
j = {"name"=>"jack",:age=>25}
j["gender"] = "male"
Iteration:
j.each{|k,v| ... }
for k,v in j
...
end
Conversion:
j.to_a => [["name", "jack"], [:age, 25]]
j.to_s => "namejackage25"
18. Data types - Range
1..10 => 1..10 (iterable, to_a)
1.0..1.5 => 1.0..1.5 (un-iterable,no to_a)
cold_war => 1945..1989
letter = 'a'..'z' => 'a'..'z' (to_a)
Iteration:
n = 3
(1..n).each{|i|print(i)} => 123
for i in (1..n)...
Membership test:
(1..100).member?(50) => true
(1..100).include?(50) => true
(1..100).cover?(2..50) => true (1.9)
19. Data types - Symbols
:symbol => :symbol
:name => :name
:'with quote' => :'with quote'
:"hello #{:name}" => :"hello name"
Conversion:
:name.to_s => "name"
"name".to_sym => :name
"name".intern => :name
"name".id2name => :name
Symbol Table:
symbols are entris in symbol table of interpreter
symbols have an index in symbol table while string have not
20. Data types - True,False,Nil
Ruby has NO Boolean type!!!
true.class => TrueClass
false.class => FalseClass
nil.class => NilClass
true.nil? => false
false.nil? => false
nil.nil? => true
Only false&nil are falsy,everything else are truthy:
puts "hi" if nil => nil
puts "hi" if false => nil
puts "hi" if true => "hi"
puts "hi" if 0 => "hi"
puts "hi" if 0.0 => "hi"
puts "hi" if "" => "hi"
puts "hi" if [] => "hi"
puts "hi" if {} => "hi"
21. Expression & Operators
Class Variable @@[_a-z][_a-zA-Z0-9]*
Instance Variable @[_a-z][_a-zA-Z0-9]*
Global Variable $[_a-z][_a-zA-Z0-9]*
Local Variable [_a-z][_a-zA-Z0-9]*
Constant [A-Z][_a-zA-Z0-9]*
class Klass;end Klass is constant!
22. Expression & Operators
Assignment:
a = [1,2,3]
Parallel assignment:
x,y,z,u = a -> x=1,y=2,z=3,u=nil
u,x,y,z = 4,*a -> u=4,x=1,y=2,z=3
Default operator:
port = port || 80 -> if port==nil then port=90
port ||= 80
Guard operator:
port = port && 8080 -> if port!=nil then port=8080
port &&= 8080
Equality:
==,!= -> equality operator
=~,!~ -> pattern-matching operator
"hello"=~/[a-z]+/
23. Expression & Operators
Boolean Operator:
&& and
|| or
! not
Conditional:
msg = "you have #{n} #{n==1? "message" : "messages"}"
defined? operator:
y = f(x) if defined? f
24. Statement & Control Structure
Conditional:
[result = ]if expression [then]
end
[result = ]if expression
elsif
else
end
if/unless as statment modifer:
... if expression
... unless expression
25. Statement & Control Structure
case:
result = case expression
when ...,... then ...
next
when ...,... then ...
break
else ...
end
Loops:(can be statement modifer)
while expression do
...
end
until expression do
...
end
for var in collection do
...
end
27. Method,Block,Proc,Lambda
Block:
One of the most powerfull feature of Ruby!
[1,2,3].each {|i| ... }
[1,2,3].each do |i|
...
end
def say_hello
print("Jack said:")
yield 1,2 if block_given?
end
say_hello{|a,b| print "hello",a,b} => Jack said:hello12
b = Proc.new{|a,b|print "hello",a,b}
l = lambda{|a,b|print "hello",a,b}
say_hello(&b)
say_hello(&l)
28. Class
The class keyword in Ruby is more like a scope operator than a
class declaration. the core job is to move you in the context of the
class, where you can define methods.
Class Constructor:
Class Person
def initialize(name)
@name = name
end
end
Person.new("Jack").name =>
undefined method `name' for #<Person:0x28406f0 @name="Jack"> (NoMethodError)
class Person
def name;return @name;end
def name=(value);@name=value;end
end
class Person
attr_reader :name
end
attr_writer , attr_reader , attr_accessor
29. Class
Class method(Static function):
Class Person
def Person.class_func
Person
end
def self.class_func ---------
end j methods
end
Method of object(singleton_method):
j = Person.new("jack")
def j.say_hi
puts "hi, i'am jack"
end ???
---------
j say_hi
Define operators:
class Person
def +();end +-*/ Person
def [];end ---------
def <=>;end
end
methods
30. every time you call a private method,
Class it must be on the implicit receiver—self.
Method Visibility:
class Person
def initialize(name,age)
@name,@age = name,age
end
def introduce
"my name is #{@name},i'm #{@age} years old"
end
protected #every method below will be protected!
def privacy;"my nick name is jacky";end
def secret;end
end
j = Person.new("jack",24)
puts j.introduce
puts j.privacy
class Person
private :privacy #Moduel.private/protected/public
private :secret
end
class Person
private *[:privacy,:secret] #OK!
end
Person.send(:private,:privacy,:secret) #OK!
31. Class
Make class on-the-fly:
Person = Class.new
[:name,:age].each do |attr|
Person.send(:attr_accessor,attr)
end
Person.send(:define_method,:hi) do
print "I'm #{@name}"
end
class Person
def bye
"bye bye!"
end
end
j = Person.new
j.name = "jack"
j.hi => "I'm jack"
j.bye => "bye bye!"
32. Class
Inheritance:
class Person;end
class Student < Person
end
s = Student.new
s.class => Student
s.class.class => Class
s.class.superclass => Person
Person.superclass => Object
Student.ancestors => [Student, Person, Object, Kernel]
33. Module
As namespace:
template<class T>
module Util class Util
PI = 3.14 {
def area public:
self.radius**2 * PI double area() {
end float r = ((T*)this)->radius;
def Util.pi2 return r*r*3.14;
PI**2 }
end };
end class Shape{};
class Pie : public Util<Pie>, public Shape
PI2 = Util.pi2 => 9.8596 {
public:
As mixin: float radius;
Pie():radius(2){}
class Shape;end };
class Pie < Shape #include <iostream>
include Util void main()
def initialize(r);@radius=r;end {
def radius;@radius;end using namespace std;
end Pie p;
cout<<p.area()<<endl;
Pie.new(2).area => 12.56 }
36. Reflection & Metaprogramming
*eval (evaluate):
eval(string[,bindings])
Klass.class_eval(&block)
Mod.mod_eval(&block)
object.instance_eval(&block)
def object.hi
puts "hi"
end
object.instance_eval do
def hi
puts "hi"
end
end alias :old_method :method
def method(*args,&block)
alias: ...
class Klass old_method(*args,&block)
def hi;puts("hi");end ...
alias :greeting,:hi end
end
40. Books & Links & Persons
Programming Ruby by Dave Thomas
The Ruby Programming Language
by David Flanagan and Yukihiro Matsumoto
The Ruby Way 3nd
by Hal Edwin Fulton
Design Patterns in Ruby by Russ Olsen
Ruby Cookbook
by Lucas Carlson and Leonard Richardson
Metaprogramming Ruby by Paolo Perrotta
41. Books & Links & Persons
YUKIHIRO DAVE THOMAS DAVID
MATSUMOTO HEINEMEIER
HANSSON