43

Ruby presentasjon på NTNU 22 april 2009

Embed Size (px)

DESCRIPTION

Introduksjon til Ruby

Citation preview

Page 1: Ruby presentasjon på NTNU 22 april 2009
Page 2: Ruby presentasjon på NTNU 22 april 2009
Page 3: Ruby presentasjon på NTNU 22 april 2009

RubyAslak Hellesøy / Øystein Ellingbø

Consulting

Page 4: Ruby presentasjon på NTNU 22 april 2009

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

Page 5: Ruby presentasjon på NTNU 22 april 2009

Ruby

Page 6: Ruby presentasjon på NTNU 22 april 2009
Page 7: Ruby presentasjon på NTNU 22 april 2009

Sapir-Whorf HypothesisPeople 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

Page 8: Ruby presentasjon på NTNU 22 april 2009

Sortering i Java

Page 9: Ruby presentasjon på NTNU 22 april 2009

Sortering i Ruby

Page 10: Ruby presentasjon på NTNU 22 april 2009

Ruby implementations

• MRI• YARV• JRuby• Rubinius• IronRuby• MagLev• MacRuby

Page 11: Ruby presentasjon på NTNU 22 april 2009

Standard libraryRubyForgeIrbRiRDocRakeRubyGemsRSpecTest::UnitRCovDebugger

Page 12: Ruby presentasjon på NTNU 22 april 2009

Object Oriented

• In Ruby, Everything is an Object

• No Primitives

• A few (seemingly) global functions

• great for scripting ...

• FUN

Page 13: Ruby presentasjon på NTNU 22 april 2009

Scripting Language

• Interpreted, not compiled

• Slower execution

• Faster development

• Implicit ‘main’ method

Page 14: Ruby presentasjon på NTNU 22 april 2009

# hello.rbputs "Hallo NTNU!"

$ ruby hello.rbHallo NTNU!$

Page 15: Ruby presentasjon på NTNU 22 april 2009

Coding Conventions def hello_world my_message = "Hi" puts my_messageend

• Two-space indentation

• No tabs

• Snake case (mostly)

Page 16: Ruby presentasjon på NTNU 22 april 2009

Variables

ClassNameTHIS_IS_A_CONSTANTlocal_variable$global_variable@instance_variableregular_methodquestion_method?dangerous_method!

Page 17: Ruby presentasjon på NTNU 22 april 2009

Variables

ClassNameTHIS_IS_A_CONSTANTlocal_variable$global_variable@instance_variableregular_methodquestion_method?dangerous_method!

Page 18: Ruby presentasjon på NTNU 22 april 2009

Everything is an Object# Classes are objectshash = Hash.new

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

# Even nil is an objecta = nila.nil? # => true

Page 19: Ruby presentasjon på NTNU 22 april 2009

Classesclass MyClassend

o = MyClass.newputs o# #<MyClass:0x355e94>

Page 20: Ruby presentasjon på NTNU 22 april 2009

Methodsclass MyClass def hello(name) "Hi, #{name}" endend

o = MyClass.newputs o.hello("Bob") # Hi, Bob

Page 21: Ruby presentasjon på NTNU 22 april 2009

Operators are methods

2 + 2

2.+(2)

list << 'a'

list.<<('a')

x == 5

x.==(5)

Page 22: Ruby presentasjon på NTNU 22 april 2009

Method argumentsdef no_argsend

def two_args(foo, bar)end

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

def with_proc(foo, &proc) proc.call("Chunky", "bacon!")end

Page 23: Ruby presentasjon på NTNU 22 april 2009

Method Signatures

def weirdo(x) if x == 5 "Bingo" else 23 endend

puts weirdo(5) # => "Bingo"puts weirdo("Sponge") # => 23

Page 24: Ruby presentasjon på NTNU 22 april 2009

Dynamic Typing

def quack_it(o) o.quackend

quack_it(duck) # OK, Duck has a quack methodquack_it(horse) # NoMethodError: undefined method 'quack'

a = 'hi'a = [] # a's type is carried by the valueputs a.class # => Array

"x" + 5 # TypeError: can't convert Fixnum into String

Page 25: Ruby presentasjon på NTNU 22 april 2009

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

Page 26: Ruby presentasjon på NTNU 22 april 2009

Open Classes

Page 27: Ruby presentasjon på NTNU 22 april 2009

Add Behaviour to Existing Classes

class Object def introduce_thyself "Hello, I am an instance of #{self.class}" endend

puts "NDC".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

Page 28: Ruby presentasjon på NTNU 22 april 2009

#1 Åpne klasserputs 5.even?puts 8.even?# false# true

Page 29: Ruby presentasjon på NTNU 22 april 2009

require 'rubygems'require 'activesupport'

puts 2.days.ago

$ ruby days.rbSat Jun 14 13:55:51 +0200 2008

Page 30: Ruby presentasjon på NTNU 22 april 2009

Collections# Arraya = [2, 3, 5]puts a[1] # => 3a << 6puts a[-1] # => 6

# Hashh = {'x' => 99, 'y' => 98}puts h['y'] # => 98h[1] = 'y'puts h[1] # => 'y'

# Ranger = 4..9puts r.to_a.inspect# [4, 5, 6, 7, 8, 9]

Page 31: Ruby presentasjon på NTNU 22 april 2009

Iterators

a = [2, 3, 5]a.each do |e| puts eend# 2# 3# 5

a = [2, 3, 5]b = a.map do |e| e * eend

puts b.inspect # [4, 9, 25]

h = {'x' => 99, 'y' => 98}h.each do |k, v| puts "#{k} => #{v}"end# x => 99# y => 98

Page 32: Ruby presentasjon på NTNU 22 april 2009

print_down_to_0(5)# 5# 4# 3# 2# 1# 0

#2 Traversering

Page 33: Ruby presentasjon på NTNU 22 april 2009

Strings

s = "A String"puts s # => A Stringputs s.object_id # => 9428360

s.gsub!("ing","and")puts s # => A Strandputs s.object_id # => 9428360

• Similar “feel” to Java and C#, except

• Mutable

• No Unicode (getting there)

Page 34: Ruby presentasjon på NTNU 22 april 2009

Symbols

x = :fooy = :foo

puts x.equal?(y) # object identity# => true

• Immutable Strings

• Mostly used as keys in hashes

• Flyweight

Page 35: Ruby presentasjon på NTNU 22 april 2009

Metaprogrammerin

def method_missing(name, *args)end

Page 36: Ruby presentasjon på NTNU 22 april 2009

r = ReverseAnything.newputs r.olleh# hello

#3 method_missing

Page 37: Ruby presentasjon på NTNU 22 april 2009

Blocks / yielddef i_yield yield 'hi'end

i_yield do |what| puts whatend# hi

def i_yield(&proc) proc.call('hi')end

Page 38: Ruby presentasjon på NTNU 22 april 2009

(1...10).each_even do |e| puts eend# 2# 4# 6# 8(1...10).each_even { |e| puts e }

#4 yielding

Page 39: Ruby presentasjon på NTNU 22 april 2009

module Happiness def happy? true endend

class RubyDeveloper include Happinessend

Modules

Page 40: Ruby presentasjon på NTNU 22 april 2009

module Awesome class Stuff endend

s = Awesome::Stuff.new

Namespaces

Page 41: Ruby presentasjon på NTNU 22 april 2009

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

Page 42: Ruby presentasjon på NTNU 22 april 2009

String text = "Aslak is 181cm tall"; Pattern p = Pattern.compile("(\\d+)cm"); Matcher m = p.matcher(text); if(m.find()) { System.out.println(m.group(1)); }

Java

text = "Aslak is 181cm tall"if text =~ /(\d+)cm/ puts $1end

Ruby

Regular ExpressionsString text = "Aslak is 181cm tall";MatchCollection m = Regex.Matches(text, @"(\d+)cm");if (m.Success){ Console.WriteLine(m[0].Groups[1].Value);}

C#

Page 43: Ruby presentasjon på NTNU 22 april 2009

Resources

http://ruby-lang.org/http://rubyforge.org/http://rubyquiz.com/

http://tryruby.hobix.com/+ thousands of blogs and lists