Beispiele



Beispiele

0 0


ruby_presentation_sdc_2013


On Github niko / ruby_presentation_sdc_2013

Warum?

"I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language."

(Yokihiro "Matz" Matsumoto)

"I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language."

(Yokihiro "Matz" Matsumoto)

Wie?

  • Perl String Features
  • Perl Syntax minus Gekröse
  • Smalltalk Type System
  • Lisp Closures, anonyme Funktionen

Features

  • interpretiert
  • Garbage-collected
  • objektorientiert
  • dynamisch typisiert
  • offene Klassen
  • alles ist ein Ausdruck
  • Closures
  • Continuations
  • mehrere Character-Encodings
  • benannte Argumente
  • nur false und nil sind nicht true-ish

Syntax

  • keine Semicolons am Zeilenende
  • Klammern nur, wo nötig
  • Implizites Return-Statement
  • Literale für Hash, Array, Range, Regexp, Wort-Array, Symbol-Array
  • Yoda-Code

Beispiele

puts -199.abs                              #=> 199
puts 'Chunky Bacon!'.length                #=> 13
puts 'ruby is cool.'.index 'u'             #=> 1
puts %q{Nice Day Isn't It?}.downcase.split('').uniq.sort.join
                                            #=> ' ?acdeinsty
            
class Fixnum
  def + other
    self - other
  end
end

p 5 + 3  #=> 2
p 5.+(3) #=> 2
            

def foo
  'klar!'
end

if a = foo
  puts a
end

b = if true
  'natürlich!'
end

puts b
puts 'klar' if true
            
(1..100).each do |i|
  puts i
end
            
def double &block
  block.call
  block.call
end

double do
  puts 'Hi!'   #=> Hi!
end            #=> Hi!
            
describe MySum do
  it "performs a sum" do
    sum = MyClass.add 1, 1
    sum.should == 2
  end
end
            
require 'sinatra'

get '/hi' do
  'Hello World!'
end
          
doc 'The schedule of a single station grouped by playlists.'
doc :example, 'http://HOSTNAME/station/deepgroove/playlists'
doc :parameters, {station_name: '[\w\d_\-]+'}
doc :key, '/station/{station_name}/playlists'
get %r{\A/station/([\w\d_\-]+)/playlists\Z} do |name|
  expires schedule_expiration, :public, :must_revalidate
  RedisStore::Playlists.find(name)
end
            

Implementierungen

Ruby-Gems

Kommandos

  • gem install GEMNAME
  • gem list
  • gem build GEMSPEC
  • gem push GEMFILE

Rake

Rakefile

task default: :test

desc 'Run all the tests'
task :test do
  ruby "test/unittest.rb"
end

Methoden

  • task
  • file
  • directory
  • namespace
  • multitask

Kommandos

  • rake
  • rake TASKNAME
  • rake -T

Bundler

Gemfile

source 'http://rubygems.org'

gem 'sinatra', '~> 0.9.0'
gem 'rack-cache'
gem 'rack-bug'
gem 'lautfm_utils', '>= 0.0.23', path: '/Users/niko/laut/lautfm_utils'
gem 'ad_machine', '>= 0.2.9', :git => 'git@github.com:lautde/ad_machine.git'

Bundler Kommandos

  • bundle install
  • bundle update GEMNAME
  • bundle pack
  • bundle install --deployment

Capistrano

Capistrano Kommandos

  • cap -T
  • cap deploy

Capfile

set :application, 'www'
set :deploy_to, '/export/webroot/www'
set :user, 'laut'

set :scm, :git
set :repository, 'git@github.com:lautde/laut.fm-V2.git'
set :branch, 'master'

namespace :deploy do
  task :restart_daemons, roles: :web do
    ...
  end

  after 'deploy', 'deploy:restart_daemons'
end

RVM

RVM Kommandos

  • curl -L https://get.rvm.io | bash
  • rvm install 2.0.0
  • rvm use 2.0.0 --default
  • rvm use 1.9.3-p285
  • rvm use jruby
  • rvm install [jruby|rbx|maglev|ironruby|macruby]

The bad, the ugly

  • Geschwindigkeit
  • Concurrency Model
  • Semantic Versioning
  • Installation
  • Tooling

Gotchas

  • Monkey Patching
  • $!, $&, $+
  • and or & || precedence
  • Regex ^...$ vs. \A...\z
  • == === #eql? #equal?
  • super vs. super()
  • 1/2 != 0.5

Links

Img Credits