the dust bunnies are my only friends 03. Jul 2008

Isobel Campbell saved him from the loneliness in his empty apartment.

 

conciseness vs. readability 01. Jul 2008

To print the contents of all files passed as arguments or in case of no arguments print the contents of stdin you could write

puts *ARGF

but isn’t

if ARGV.empty?
  puts $stdin.readlines
else
  ARGV.each do |filename|
    puts File.readlines(filename)
  end
end

much more readable?

 

read file with one line of code 01. Jul 2008

There are probably at least a have a dozen ways to print the contents of a file in Ruby, here I will present four ways of doing it.

The first method is rather verbose. Assign a file object and iterate on the file object with the each_line method and then close the file.

f = open('file.txt')
f.each_line do |line|
  puts line
end
f.close

The second method still assigns a file object, but then uses the more compact readlines instance method and closes the file.

f = open('file.txt')
puts f.readlines
f.close

The third method uses block form to access the file and automatically close it.

open('file.txt') { |f| puts f.readlines }

The fourth method calls the class method readlines to wrap it all up in one statement.

puts File.readlines('file.txt')
 

the awful bar 30. Jun 2008

So schnell ich die neue Version des Firefox auf der Festplatte hatte, so schnell habe ich sie auch wieder gelöscht. Das KO-Kriterium: Die “smart location bar” auch genannt “awesome bar”. Ein absolutes Usability-Desaster und gutes Beispiel für gut gemeint, aber nicht gut gemacht. Nehmen wir an der Browser-Cache ist leer und ich gebe eine Adresse in die Adressezeile ein. Danach gebe ich dann jeweils den Anfang von der Subdomain, der Domain und des Seitentitels ein und vergleiche die Ausgaben der verschiedenen Firefox-Versionen.

Bei der Subdomain liefert der Firefox 2 das gewünschte Ergebnis,

Firefox 2 mit Subdomain

ebenso wie der Firefox 3

Firefox 3 mit Subdomain

Bei der Domain verhält sich der Firefox 2 erwartungskonform

Firefox 2 mit Domain

Aber im Firefox 3 wird nun unsinnigerweise die eingegebene Adresse angezeigt.

Firefox 3 mit Domain

Beim Seitentitel hält sich der Firefox 2 vornehm zurück,

Firefox 2 mit Seitentitel

aber der Firefox 3 meint einen mit unnützen Ergebnissen belästigen zu müssen.

Firefox 3 mit Seiten

Das ganze wäre ja nicht weiter schlimm, wenn man den Unsinn einfach mit dem klicken eines Kontrollkästchens abschalten könnte, aber nein es gibt weder eine Option, noch eine versteckte Option und auch kein Plugin welches dieses widersinnige Verhalten abstellt. Hier eine Liste der Optionen die empfohlen werden, aber nicht funktionieren.

browser.urlbar.matchBehavior = 0
Browser.urlbar.richResults = false
browser.urlbar.maxRichResults = 0
browser.urlbar.matchOnlyTyped = true

Ebenfalls keine Linderung bringen oldbar, Old Location Bar und Hide Unvisited 3.

Das einzige was erfolgreich Abhilfe gegen dieses unsinnige Verhalten schafft ist ein Downgrade und das ist gar nicht so einfach wenn die alte Version bei der Installation überschrieben wurde, denn wenn man nach Firefox 2 googelt findet man alles andere nur nicht den Firefox 2. Ferne ist eine Beschwerde bei der Mozilla Foundation anzuraten, damit der Unsinn so schnell wie möglich entfernt wird oder zumindest deaktivierbar wird.

 

the beauty of ruby 1.9 28. Jun 2008

Let’s say we have a string and three arrays

string = "672"
java = []
ruby18 = []
ruby19 = []

Now you want to convert the string to an array of integers. If you were writing Ruby in javastyle you would probably write something like

i=0
digits = string.split(//)
while i < digits.size do
  java << digits[i].to_i
  i+=1
end

but since we’re we’re doing Ruby in rubystyle it looks more like

string.split(//).each { |digit| ruby18 << digit.to_i }

which looks much nicer, but still not quite right, but with Ruby 1.9’s chars interator it gets even better.

string.chars { |digit| ruby19 << digit.to_i }

Goes down like butter, doesn’t it? Isn’t that how it always should have been? And the good thing is, there is lots of this good stuff in Ruby 1.9. Maybe in Ruby 2.0 there will even be a to_i method in the array class, but now I’m getting carried away.

 

1 ... 7 8 9 10 11 ... 316