4. Yukihiro “Matz”
Matsumoto
1993-02-24: Matz starts hacking on Ruby
1995-12-21: Matz released Ruby on fj.sources
1999: First book in Japanese
2000: First book in English
7. Sapir-Whorf Hypothesis
People who speak different
languages perceive and think
about the world quite differently
People's thoughts are determined by the
categories made available by their language
Differences among languages cause
differences in the thought of their speakers
Any language that does change the way you
think about programming isn't worth learning
22. Method arguments
def no_args
end
def two_args(foo, bar)
end
def varargs(foo, *bar)
puts bar.class # => Array
end
def with_proc(foo, &proc)
proc.call(quot;Chunkyquot;, quot;bacon!quot;)
end
23. Method Signatures
def weirdo(x)
if x == 5
quot;Bingoquot;
else
23
end
end
puts weirdo(5) # => quot;Bingoquot;
puts weirdo(quot;Spongequot;) # => 23
24. Dynamic Typing
quot;xquot; + 5 # TypeError: can't convert Fixnum into String
a = 'hi'
a = [] # a's type is carried by the value
puts a.class # => Array
def quack_it(o)
o.quack
end
quack_it(duck) # OK, Duck has a quack method
quack_it(horse) # NoMethodError: undefined method 'quack'
25. It is still Typing
• Objects are still strongly typed
• You can not tell an object to be another
type
• But you can tell a variable to point to
an object of a different type
• You can ask an object its type
• But don’t
• Instead, ask what it can do
27. Add Behaviour to
Existing Classes
class Object
def introduce_thyself
quot;Hello, I am an instance of #{self.class}quot;
end
end
puts quot;NDCquot;.introduce_thyself
# => Hello, I am an instance of String
puts 5.introduce_thyself
# => Hello, I am an instance of Fixnum
puts lambda {}.introduce_thyself
# => Hello, I am an instance of Proc
31. Iterators
a = [2, 3, 5]
a = [2, 3, 5]
b = a.map do |e|
a.each do |e|
e*e
puts e
end
end
#2
puts b.inspect
#3
# [4, 9, 25]
#5
h = {'x' => 99, 'y' => 98}
h.each do |k, v|
puts quot;#{k} => #{v}quot;
end
# x => 99
# y => 98
33. Strings
• Similar “feel” to Java and C#, except
• Mutable
• No Unicode (getting there)
s = quot;A Stringquot;
puts s # => A String
puts s.object_id # => 9428360
s.gsub!(quot;ingquot;,quot;andquot;)
puts s # => A Strand
puts s.object_id # => 9428360
34. Symbols
• Immutable Strings
• Mostly used as keys in hashes
• Flyweight
x = :foo
y = :foo
puts x.equal?(y) # object identity
# => true
41. Regular Expressions
• Find words and patterns in strings
• Validation
• Extract substrings
• Used heavily in Ruby, Perl, Python and Javascript
• b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}b
42. Regular Expressions
String text = quot;Aslak is 181cm tallquot;;
MatchCollection m = Regex.Matches(text, @quot;(d+)cmquot;);
C#
if (m.Success)
{
Console.WriteLine(m[0].Groups[1].Value);
}
String text = quot;Aslak is 181cm tallquot;;
Pattern p = Pattern.compile(quot;(d+)cmquot;);
Java Matcher m = p.matcher(text);
if(m.find()) {
System.out.println(m.group(1));
}
text = quot;Aslak is 181cm tallquot;
Ruby
if text =~ /(d+)cm/
puts $1
end
43. Resources
http://ruby-lang.org/
http://rubyforge.org/
http://rubyquiz.com/
http://tryruby.hobix.com/
+ thousands of blogs and lists