Wrap and iterate with the power of yield

Iterating over a collection isn’t rocket science. Obviously if you need to iterate over a really big collection you may need to adjust your strategy but it is something that in the vast majority of programming languages you get quite for free.

In Ruby for example you can just send the each message to your collection together with a block of code. The block will be executed for each element of the collection itself.

But what if you need “something more” from the objects you’re iterating on? If, for example, each element of your initial collection is an array and you want to abstract away how you access each of its elements? What options do you have?

Continue reading “Wrap and iterate with the power of yield”

Check PHP syntax error in PHP.

This sounds much like “Inception” right?

Well a week ago I had the need to implement a PHP syntax check in PHP, to be more precise inside a PrestaShop (PS) module.

After a little bit of “googling” I stumbled upon a StackOverflow comment showing the solution I needed. Actually I missed the original link so I’m posting this pearl as a “bump” just to help spread the solution. I don’t own any credits 😉

Continue reading “Check PHP syntax error in PHP.”

A little bit of metaprogramming with PHP

I should admit it. After writing more and more Ruby code and get into its beautiful Object Model it feels rather strange to go back to PHP.

What I miss mostly is the syntactic sugar and the less verbose syntax.

You don’t have to put dollar signs before variables names, semicolons at the end of statements and most of the times you can also omit method parentheses (i.e. when there are no ambiguities).

But beware! Omitted parentheses could lead to unexpected behaviors and bugs that could be hard to find! So you’d better indulge on this sugar only if you know what you’re doing.

Continue reading “A little bit of metaprogramming with PHP”

Initialize Ruby Objects with hashes

Ruby is a wonderful language (personal opinion! :P) and one of its main features is that it strongly adopts the concepts of Object Oriented Paradigm (OOP).

More than a few times I found it compared to Smalltalk for how strongly it aligns with the over mentioned concepts.

One of the basic good practices on which the OOP funds its roots is to minimize dependencies between objects.

Continue reading “Initialize Ruby Objects with hashes”