robsite

In productivity

Object#andand

Praktisches gem von Reg Braithwaite mit dem Methoden-Aufrufe auf nil-Objekten abgefangen werden können.

# Ohne andand:
@user = User.find(1)
@address = @user ? @user.address : nil
# Mit andand:
@address = User.find(1).andand.address

Enthält außerdem noch tap zum leichten debugging:

# Original:
p [1, 2, 3].reverse.map {|x| x**2 }
# Debug durch Auseinanderrupfen und Zwischenvariable:
bla = [1, 2, 3].reverse
p bla
p bla.map {|x| x**2 }
# Debug mit tap:
p [1, 2, 3].reverse.tap {|d| p d }.map {|x| x**2 }

Einfach sudo gem install andand und

require 'rubygems'
require 'andand'

in den Code.

Ähnlich praktisch ist der tee-Befehl auf der Commandline:

ps aux | tee p.txt | more

ps aux gibt alle laufenden Prozesse aus, tee p.txt schreibt die Ausgabe von ps aux in die Datei p.txt und gibt sie weiter an more.

Update:

Rails 2.3 hat andand-ähnliche Funktionalität schon eingebaut: try

@address = User.find(1).try(:address) 

Mehr dazu: ozmm.org/posts/try.html

ongoing · Understand Your User

It’s like this: There’s only one person in the world whose needs and problems you really understand and whom you know exactly how to satisfy: that would be you. So build something that you use all the time, and, unless you’re really weird and different from everyone else, you’ve got a potential winner.

· productivity · ★
Mastodon