![]() |
ActionPack 1.12.0 ActiveRecord 1.14.0 ActiveSupport 1.3.0 | RailTies 1.1.0 Prototype 1.5.0 Scriptaculous 1.6.0 |
![]() |
![]() |
Neues in ActiveSupport 1.3.0Object – Erweiterungen für alle Ruby-Objektewith_optionsDie neue Methode ActionController::Routing::Routes.draw do |map| # Routes für Account map.with_options(:controller => 'account') do |account| account.home '', :action => 'dashboard' account.signup 'signup', :action => 'new' account.logout 'logout', :action => 'logout' end # erzeugt die Aufrufe map.home '', :action => 'dashboard', :controller => 'account' map.signup 'signup', :action => 'new', :controller => 'account' map.logout 'logout', :action => 'logout', :controller => 'account' end Vorsicht: In der aktuellen Implementation überschreiben die allgemeinen Optionen die speziellen, also: $stdout.with_options(:test => 'allgemein') do |foo| foo.p(:test => 'speziell') #-> {:test=>"allgemein"} end Es gibt dafür einen Patch. to_jsonJedes Objekt kann mit
extended_by
class Foo include Enumerable end foo = Foo.new foo.extend Comparable foo.extended_by #-> [Comparable, Enumerable]
x = 'dings' x.extend_with_included_modules_from foo x.is_a?(Comparable) && x.is_a?(Enumerable) #-> true copy_instance_variables_from
Module und KlassenDelegation (= Weiterleiten von Botschaften an andere Objekte) funktioniert jetzt nicht nur für einfache Objekte, sondern für beliebige Ausdrücke. Simples Beispiel: class Schaf def test 'Määäh!' end end class Wolf delegate :test, :to => 'Schaf.new' end Wolf.new.test #-> "Määäh!" Beispiel für ein Model: class Account < ActiveRecord::Base has_one :subscription delegate :free?, :paying?, :to => :subscription delegate :overdue?, :to => "subscription.last_payment" end test account.free? # => account.subscription.free? account.overdue? # => account.subscription.last_payment.overdue? Symbol#to_procDas ist grandios: Einfache Blöcke mit # map: 'meine gitarre ist kaputt'.split.map(&:capitalize).join(' ') #-> "Meine Gitarre Ist Kaputt" # Sortieren nach Größe: puts %w'Yodas Grammatik ist ziemlich schlecht'. sort_by(&:size).reverse.join(' ') #-> Grammatik ziemlich schlecht Yodas ist # Alle Administratoren finden: @admins = User.find(:all).select(&:admin?) @admins.each(&:save) Zum Thema gibt es einen Artikel in der Ruby-Mine. Enumerablegroup_byDie Methode Pupil.find(:all).group_by(&:age) #-> {10 => ['Klaus', 'Nadin'], 11 => ['Julian', ...], ...} in_groups_ofArrays haben jetzt eine Methode Array(1..9).in_groups_of(4) {|g| p g} #-> [1, 2, 3, 4] #-> [5, 6, 7, 8] #-> [9, nil, nil, nil] to_xmlHash und Array (und ActiveRecord::Base)
besitzen nun die Methode Post.find(:all).to_xml(:include => :tags) ergibt <?xml version="1.0" encoding="UTF-8"?> <entries> <entry> <tags> <tag> <entry-id>1</entry-id> <title>Ruby</title> <tag-id>1</tag-id> <id type="integer">1</id> </tag> </tags> <title>Ruby ist toll!</title> <id type="integer">1</id> <content>Ruby ist eine dynamische Scriptsprache aus Japan.</content> <is-public type="boolean">true</is-public> <created-at type="datetime">2006-03-30T22:18:13+02:00</created-at> </entry> <entry> <title>Hunger</title> <id type="integer">2</id> <content>Ich brauche was zu Essen</content> <is-public type="boolean">false</is-public> <created-at type="datetime">2006-03-30T22:18:13+02:00</created-at> </entry> </entries> WeiteresLoggerMit logger.around_info "Starte Tierversuch...", "Tierversuch beendet." do for tier in tiere tier.test end end Kleinere Änderungen
Ei::KOCHZEIT = 3.minutes + 30.seconds
|