I love “map by pluralisation” [now: map_by_method]

Posted by Dr Nic on October 04, 2006

Update: this is a gem called map_by_method.

The other day I introduced a new syntax idea that I call “map by pluralisation”. Everyday I use it in code and in the console/irb I fall more in love with its simplicity - both to type and to read.

The following are all equivalent:

>> BankTransaction.columns.names  # map by pluralisation
=> ["id", "amount", "date", "description", "balance"]
>> BankTransaction.columns.name   # singular works too
=> ["id", "amount", "date", "description", "balance"]
>> BankTransaction.columns.map_name  # merge collector (map) + operation (name is method of BankTransaction object)
=> ["id", "amount", "date", "description", "balance"]
>> BankTransaction.columns.collect_name  # merge collector (collect) + operation
=> ["id", "amount", "date", "description", "balance"]

All of which are easier to read and quicker to type than the current equivalents:

>> BankTransaction.columns.map {|p| p.name}  # standard map
=> ["id", "amount", "date", "description", "balance"]
>> BankTransaction.columns.map &:name  # Symbol.to_proc
=> ["id", "amount", "date", "description", "balance"]

You can now type:

>> BankTransaction.columns.select_primary  # merge collector (select) and operator (primary returns true/false on a Column object)
=> [#<...MysqlColumn:0x3c8a4f0 @limit=11, @sql_type="int(11)", @primary=true, @type=:integer, @number=true, @name="id">]

Instead of:

>> BankTransaction.columns.select {|c| c.primary}
>> BankTransaction.columns.select &:primary

Use with ActiveRecords

“Map by pluralisation” is truly wonderful in the console for exploring and collecting data models.

Without knowing a thing about the data model, I bet you can understand either of the following:

@transactions = BankTransaction.find :all, :conditions => ['date = ?', Date.today], :include => [:accounts => [:owner]]
return @transactions.collect_accounts.select_overdrawn?.collect_owner.full_names

Answer: the full name of each owner of an overdrawn bank accounts for all today’s bank transactions.

It reads well as there is a minimum of {, }, |, &, : characters.

Download

Code available here

Tip for use with ActiveRecord Associations

You may need to cast associations to Arrays.

@account.transactions.to_a.map_dates

The result of the transactions assocation on the Account class is not an Array. My initial attempts to provide the “map by pluralisation” code (a method_missing) didn’t work, and casting it to an explicit Array using to_a seemed simple and harmless.

Discussion of Syntax Ideas

Ruby Forum

Extending Rails is like converting a Mini Cooper into a Rocket Car

Posted by Dr Nic on October 03, 2006

Not only is Ruby on Rails open sourced so all the code is there for you to read over, learn from, patch and improve, but its built on Ruby, so it is inherently capable of being extended, patched, improved and boosted. Say, like converting a Mini Cooper into a Rocket Car [below]. No one’s going to stop you - you just do it when you feel like it.

[Author Note: I am conscious that the preceding paragraph is a poor attempt to justify showing you the following video of a Mini Cooper being sent down a ski ramp in Norway on the Top Gear show.]

Highlights from the video:

Jeremy: “Gravity’s a cruel and unpredictable mistress”
James: “No its not, its a constant all over the world!”

Jeremy: “this is actually rocket science”

The use of the A-team TV theme whilst they build the rocket car

Richard: “we are at the cutting edge of cocking about”

zip vs transpose

Posted by Dr Nic on October 03, 2006

Prelude

If I wanted to create a Hash from two arrays - one containing the keys and the other the values - I have used transpose:

>> keys = %w(name description country)
>> values = ["Dr Nic", "Good lookin'", "Netherlands"]
>> [keys, values].transpose
=> [["name", "Dr Nic"], ["description", "Good lookin'"], ["country", "Netherlands"]]
>> hash = Hash[*[keys, values].transpose.flatten]
=> {"name"=>"Dr Nic", "country"=>"Netherlands", "description"=>"Good lookin'"}

I was happy with that.

The newcomer from outta town…

Today I found zip for Arrays.

>> keys.zip(values)
=> [["name", "Dr Nic"], ["description", "Good lookin'"], ["country", "Netherlands"]]
>> hash = Hash[*keys.zip(values).flatten]
=> {"name"=>"Dr Nic", "country"=>"Netherlands", "description"=>"Good lookin'"}

Now I don’t know which is better. Nor do I know which syntax is cleaner and more meaningful.

Both are as meaningless as each other:

  • How many people remember what transpose means from matrix mathematics at school anyway?
  • How many people would look at keys.zip(values) and guess what the result will be?

Anyone with any thoughts on this?

[Story] The teacher’s job is simple: Ask the right questions

Posted by Dr Nic on October 03, 2006

I’ve lived in India and one thing is immediately apparent: they’ve got more people than you do. And most of them are poor, by our standards. Poor people don’t get to use computers, do they?

One very interesting experiment showed what happens when you install computers connected to the Internet in “holes in the wall”, and observed what they did. The article is an interview with the man who setup the experiment, and covers subsequent experiments he ran, such as “Can year 9 students answer questions from the year 10 exam if you give them 2 hours and the internet?

It leads the introduction of the term “minimally invasive education”. For the most part, this is just how you learnt Ruby, Javascript or anything else since you left school. I bet you are a better student now than you were then too. That’s not the case for most adults - see the last quote below.

It is a fascinating article on learning, on how children learn, and on the consequences of 1.2 billions Indians “going online”.

Interesting quotes

the most avid users of the machine were ghetto kids aged 6 to 12, most of whom have only the most rudimentary education and little knowledge of English. Yet within days, the kids had taught themselves to draw on the computer and to browse the Net.

If the adult was simply underestimating the child’s ability to cope with a computer, then that should happen with any child. And I asked myself, “Why then would we want to use the same teaching methods for children as we use for teaching adults?”

I went to a middle-class school and chose some ninth graders, two girls and two boys. I called their physics teacher in and asked him, “What are you going to teach these children next year at this time?” He mentioned viscosity. I asked him to write down five possible exam questions on the subject. I then took the four children and said, “Look here guys. I have a little problem for you.” They read the questions and said they didn’t understand them, it was Greek to them. So I said, “Here’s a terminal. I’ll give you two hours to find the answers.” … They answered all five questions in two hours.

What is important is infrastructure and access … The teacher’s job is very simple. It’s to help the children ask the right questions.

you can multiply the effectiveness of 10 teachers by 100 - or 1,000 - fold if you give children access to the Internet.

There are 50 or 60 million cable-TV connections in India at this point in time. The guys who set up the meters, splice the coaxial cables, make the connection to the house, etc., are very similar to these kids. They don’t know what they’re doing.

The only reaction we got from adults was, “What on earth is this for? Why is there no one here to teach us something? How are we ever going to use this?” I contend that by the time we are 16, we are taught to want teachers, taught that we cannot learn anything without teachers.

[Wish] Spam filter by Language

Posted by Dr Nic on October 01, 2006

Does anyone know to whom I address and send this letter?

Dear Gmail,

Thank you for Gmail. I love it and would hug it if it were a teddy bear and no one was looking.

Recently, some Asian-origin spammers have included me in their lists. A while back, it was some Russian spammers, or thereabouts.

If the contents of my emails don’t fit into the basic English ASCII set of characters, plus some of those European characters with the cute accents on the tops, then the email probably isn’t one I can read, thus spam or not, I’m just not that interested.

I doubt I’m alone in my mono-linguistic capabilities either.

Either help me filter out foreign language spam, or let me translate it and then with confidence I can say “No I don’t want a chinese nor russian penis enlarger, but thanks for asking”.

Yours sincerely,
Dr Nic