Ruby
Aslak Hellesøy / Øystein Ellingbø
               Consulting
Yukihiro “Matz”
             Matsumoto
  1993-02-24: Matz starts hacking on Ruby

1995-12-21: Matz released Ruby on fj.sou...
Ruby
Sapir-Whorf Hypothesis
People who speak different
languages perceive and think
about the world quite differently

        ...
Sortering i Java
Sortering i Ruby
Ruby implementations
      • MRI
      • YARV
      • JRuby
      • Rubinius
      • IronRuby
      • MagLev
      • MacRu...
Standard library
RubyForge
Irb
Ri
RDoc
Rake
RubyGems
RSpec
Test::Unit
RCov
Debugger
Object Oriented

• In Ruby, Everything is an Object
 • No Primitives
 • A few (seemingly) global functions
    • great for...
Scripting Language
• Interpreted, not compiled
 • Slower execution
 • Faster development
• Implicit ‘main’ method
# hello.rb
puts quot;Hallo NTNU!quot;


$ ruby hello.rb
Hallo NTNU!
$
Coding Conventions
    def hello_world
      my_message = quot;Hiquot;
      puts my_message
    end


   • Two-space inde...
Variables
ClassName
THIS_IS_A_CONSTANT
local_variable
$global_variable
@instance_variable
regular_method
question_method?
...
Variables
ClassName
THIS_IS_A_CONSTANT
local_variable
$global_variable
@instance_variable
regular_method
question_method?
...
Everything is an Object
   # Classes are objects
   hash = Hash.new

   # Numbers are objects
   -1.abs # => 1

   # Even ...
Classes
class MyClass
end

o = MyClass.new
puts o
# #<MyClass:0x355e94>
Methods
class MyClass
  def hello(name)
    quot;Hi, #{name}quot;
  end
end

o = MyClass.new
puts o.hello(quot;Bobquot;)
#...
Operators are methods
 2+2               list << 'a'

 2.+(2)            list.<<('a')




          x == 5

          x.==...
Method arguments
def no_args
end

def two_args(foo, bar)
end

def varargs(foo, *bar)
  puts bar.class # => Array
end

def ...
Method Signatures
def weirdo(x)
  if x == 5
    quot;Bingoquot;
  else
    23
  end
end

puts weirdo(5)        # => quot;B...
Dynamic Typing
quot;xquot; + 5 # TypeError: can't convert Fixnum into String



a = 'hi'
a = []       # a's type is carrie...
It is still Typing
• Objects are still strongly typed
 • You can not tell an object to be another
    type
   • But you ca...
Open Classes
Add Behaviour to
    Existing Classes
class Object
  def introduce_thyself
    quot;Hello, I am an instance of #{self.clas...
puts 5.even?
puts 8.even?
# false
# true
require 'rubygems'
 require 'activesupport'

 puts 2.days.ago

$ ruby days.rb
Sat Jun 14 13:55:51 +0200 2008
Collections
                      # Range
# Array
                      r = 4..9
a = [2, 3, 5]
                      puts ...
Iterators
                    a = [2, 3, 5]
a = [2, 3, 5]
                    b = a.map do |e|
a.each do |e|
             ...
print_down_to_0(5)
#5
#4
#3
#2
#1
#0
Strings
• Similar “feel” to Java and C#, except
 • Mutable
 • No Unicode (getting there)
    s = quot;A Stringquot;
    pu...
Symbols
• Immutable Strings
• Mostly used as keys in hashes
• Flyweight
x = :foo
y = :foo

puts x.equal?(y) # object ident...
Metaprogrammerin

 def method_missing(name, *args)
 end
r = ReverseAnything.new
puts r.olleh
# hello
Blocks / yield
                    def i_yield(&proc)
def i_yield
                      proc.call('hi')
  yield 'hi'
     ...
(1...10).each_even do |e|
  puts e
end
#2
#4
#6
#8
(1...10).each_even { |e| puts e }
Modules
module Happiness
  def happy?
    true
  end
end

class RubyDeveloper
  include Happiness
end
Namespaces
module Awesome
  class Stuff
  end
end

s = Awesome::Stuff.new
Regular Expressions

• Find words and patterns in strings
 • Validation
 • Extract substrings
• Used heavily in Ruby, Perl...
Regular Expressions
String text = quot;Aslak is 181cm tallquot;;
MatchCollection m = Regex.Matches(text, @quot;(d+)cmquot;...
Resources

     http://ruby-lang.org/
    http://rubyforge.org/
    http://rubyquiz.com/
  http://tryruby.hobix.com/
+ tho...
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Upcoming SlideShare
Loading in …5
×

Ruby presentasjon på NTNU 22 april 2009

618 views

Published on

Introduksjon til Ruby

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
618
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby presentasjon på NTNU 22 april 2009

  1. 1. Ruby Aslak Hellesøy / Øystein Ellingbø Consulting
  2. 2. 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
  3. 3. Ruby
  4. 4. 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
  5. 5. Sortering i Java
  6. 6. Sortering i Ruby
  7. 7. Ruby implementations • MRI • YARV • JRuby • Rubinius • IronRuby • MagLev • MacRuby
  8. 8. Standard library RubyForge Irb Ri RDoc Rake RubyGems RSpec Test::Unit RCov Debugger
  9. 9. Object Oriented • In Ruby, Everything is an Object • No Primitives • A few (seemingly) global functions • great for scripting ... • FUN
  10. 10. Scripting Language • Interpreted, not compiled • Slower execution • Faster development • Implicit ‘main’ method
  11. 11. # hello.rb puts quot;Hallo NTNU!quot; $ ruby hello.rb Hallo NTNU! $
  12. 12. Coding Conventions def hello_world my_message = quot;Hiquot; puts my_message end • Two-space indentation • No tabs • Snake case (mostly)
  13. 13. Variables ClassName THIS_IS_A_CONSTANT local_variable $global_variable @instance_variable regular_method question_method? dangerous_method!
  14. 14. Variables ClassName THIS_IS_A_CONSTANT local_variable $global_variable @instance_variable regular_method question_method? dangerous_method!
  15. 15. Everything is an Object # Classes are objects hash = Hash.new # Numbers are objects -1.abs # => 1 # Even nil is an object a = nil a.nil? # => true
  16. 16. Classes class MyClass end o = MyClass.new puts o # #<MyClass:0x355e94>
  17. 17. Methods class MyClass def hello(name) quot;Hi, #{name}quot; end end o = MyClass.new puts o.hello(quot;Bobquot;) # Hi, Bob
  18. 18. Operators are methods 2+2 list << 'a' 2.+(2) list.<<('a') x == 5 x.==(5)
  19. 19. 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
  20. 20. Method Signatures def weirdo(x) if x == 5 quot;Bingoquot; else 23 end end puts weirdo(5) # => quot;Bingoquot; puts weirdo(quot;Spongequot;) # => 23
  21. 21. 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'
  22. 22. 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
  23. 23. Open Classes
  24. 24. 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
  25. 25. puts 5.even? puts 8.even? # false # true
  26. 26. require 'rubygems' require 'activesupport' puts 2.days.ago $ ruby days.rb Sat Jun 14 13:55:51 +0200 2008
  27. 27. Collections # Range # Array r = 4..9 a = [2, 3, 5] puts r.to_a.inspect puts a[1] # => 3 # [4, 5, 6, 7, 8, 9] a << 6 puts a[-1] # => 6 # Hash h = {'x' => 99, 'y' => 98} puts h['y'] # => 98 h[1] = 'y' puts h[1] # => 'y'
  28. 28. 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
  29. 29. print_down_to_0(5) #5 #4 #3 #2 #1 #0
  30. 30. 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
  31. 31. Symbols • Immutable Strings • Mostly used as keys in hashes • Flyweight x = :foo y = :foo puts x.equal?(y) # object identity # => true
  32. 32. Metaprogrammerin def method_missing(name, *args) end
  33. 33. r = ReverseAnything.new puts r.olleh # hello
  34. 34. Blocks / yield def i_yield(&proc) def i_yield proc.call('hi') yield 'hi' end end i_yield do |what| puts what end # hi
  35. 35. (1...10).each_even do |e| puts e end #2 #4 #6 #8 (1...10).each_even { |e| puts e }
  36. 36. Modules module Happiness def happy? true end end class RubyDeveloper include Happiness end
  37. 37. Namespaces module Awesome class Stuff end end s = Awesome::Stuff.new
  38. 38. 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
  39. 39. 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
  40. 40. Resources http://ruby-lang.org/ http://rubyforge.org/ http://rubyquiz.com/ http://tryruby.hobix.com/ + thousands of blogs and lists

×