Posts Tagged ‘ruby’

It is not chrismas, hence no quiz, but this was strangely surprising:

class A
@@t = "A::@@t"
T="A::T"

def self.s1; @@t; end
def self.s2; T; end
end

class B < A
def self.s1; @@t; end
def self.s2; T; end
end

class C < A
@@t = "C::@@t"
T="C::T"
def self.s1; @@t; [...]


This benchmark compares thinking_sphinx with acts_as_xapian. We need a search engine that gives us the IDs of matching documents from a fulltext index, basic text search only.
Data

one table with 200k entries with 5k of text (avg) in one column
one table with 500k entries with 7k of text (avg) in 6 columns
one table with 500k entries [...]


With just that small addition:

class Symbol
alias_method :old_eeq, :”===”

def ===(obj)
(obj.respond_to?(self) && obj.send(self)) ||
old_eeq(obj)
end
end

you can do things like

case current_user
when :admin? then “admin”
when :regular? then “something_for_regulars”
when :guest? then “do_guest_stuuff”
else “huh”
end

Isn’t that cool much more readable?


I lamented earlier about the absence of a racecondition-free find_or_create implementation and somewhat promised to check back with a working implementation. YES, WE CAN: NOW is the time, folks!
As stated there the first thing missing is an advisory locking implementation. With the database as the natural choice for a synchronisation point that implementation [...]


Ever done Erlang? Now I wondered how flexible ruby would be and if an erlang style pattern matcher can be done in ruby. As it turns out, I came pretty close:
You might recognize this:

m = matching! [ 1, [ :a, 2 ], 3 ] do
on [ a ] [...]


…or: There is a difference between methodname(args) and self.methodname(args)!
The way how ruby deals with private, protected and public messages was a bit unclear to me. So I decided to dig a bit into it and to find out how it is really working. Finally I have irb installed: so lets go and explore.
This pastie shows [...]


for all of you. Before you enter the New Years Day frenzy, here are the results of last week’s puzzler. (Haven’t been a big secret anyways: every one with a Ruby interpreter could know in the meantime. Yes, that means you!)
What is the output of the following?

~ > irb
>> puts false or 1
false
=> 1
>> puts [...]


…some piece of ruby code that might not behave as you think (depending on your thinking, of course.). Do you know the results?

Note: I finally managed to find the option that allows you to comment on my blog without being logged into wordpress.com. Feel free to comment your findings below.

What is the output of [...]


Everyone seems to know that building an URL from a hash has some speed issues. The reason for that is quite clear: it looks up the shortest route using routes.rb, no wonder that this would take some time.
But how slow is it really? Well, I ran some tests and compared that against building a [...]


Say you are running a bookstore. (Didn’t you know that Rails is all about selling books?). Like everybody else you moved over to an IT infrastructure for managing sales during the last years, and now you have 5000 customers in the database and know about all the books these guys bought at your store – [...]