Sunday, October 19, 2008

Recursively replace all files with tabs in Linux

Ok I don't want to forget this ever again so I will write it here.

sed 's/ //g' `find -type f`

  • Note I'm using both the grave accent "`" and the apostrophe "'". "`" is used to create a special kind of shell escape that substitutes each line of the output in the place you inserted it. So sed will be called once for each file found by find.
    It's the same as doing manually:

    sed 's/ //g' ./file1.txt
    sed 's/ //g' ./file2.txt
    sed 's/ //g' ./dir/file2.txt
    .
    .
    .

  • To type the tab inside the sed regexp hit ctrl-v and then the tab key.
  • find -type f will show in each line the path of each file recursively.

Friday, October 10, 2008

Auto Focusable Forms

Just commited a Rails plugin (my first one, yay!!) to give automatic focus to the first input of the first form in each of your views.

Of course you can disable this behaviour but I think that having focus in the first input by default can be very useful.

You can read more in the project page here.

Tuesday, October 7, 2008

Internet Explorer 7 accept header and Rails respond_to

If you browse through IE to an url that should spit html but instead you see the feed reader page (the one with the text "You are viewing a feed that contains frequently updated content") then just be sure that the page's respond_to has the html mime type as its first item instead of atom or rss.

This solution addresses the way IE constructs its http request accept header which accepts any MIME type you may be serving so the first one available will be accepted which is not necessarily the html you are expecting.
Firefox doesn't have this problem because it explicitly states what it wants.

So in summary, change this:

respond_to do |format|
format.atom
format.html
end
to this:

respond_to do |format|
format.html
format.atom
end

Monday, October 6, 2008

One thing to keep in mind when extending Ruby classes

I was reading this post in Mauricio Fernandez's blog (rcov) and I got surprised about the behavior I'm summarizing in the following code:
class A
def foo
"A#foo"
end

def singleton_class
class << self
self
end
end
end

a = A.new
puts a.foo #=> A#foo
puts a.singleton_class.ancestors.inspect #=> [A, Object, Kernel]

module X
def foo
"X#foo"
end
end

class A
include X
end

a.extend X
puts a.foo #=> A#foo (and not X#foo!!!!!!!)
puts a.singleton_class.ancestors.inspect #=> [A, X, Object, Kernel] (and not [X, A, X, Object, Kernel])

So remember, you cannot extend or include a class with a module that is already present in the ancestors chain, it will be ignored!