Alistapart Survey

- 2008-07-29 14:47:24

Another Concise Code Example

- 2008-04-26 15:54:09

This is just another example showing how I refactor code down to its bare minimum. The reason why I do this so much (and indeed I think why ruby is so easy to read compared to other languages) is because it makes my code more readable and less of a bugger to pick up after a while.

class Page
    attr_accessor :parent_id

    def old_parent
    # The returns aren't needed here in ruby, but in other
    # languages using this logic block you would require
    # the returns, so I left them in here.

    if self.parent?
      return Page.find( self.parent_id )
    else
      return false
    end
  end

  def parent
    return Page.find( self.parent_id ) if self.parent?
    false
  end

end

old_parent and parent return exactly the same, but one is 2 lines compared to 5 and easier to read.

Update: Ciaran pointed out that the Page.parent method would only ever return false. Added the return statement to it to fix the bug.

Inaugural Habaricon

- 2008-04-01 07:27:55

So I managed to secure a place to HabariCon '08. Not quite sure what to expect, but being able to meet other people in real life and talk about habari & related things all day will be quite cool.

Expect a round-up post of the 'con tonight!

BarCamp Manchester

- 2008-03-01 11:34:25

So I'm at BarCamp Manchester for the day. Its great meeting people I already know, and meeting new ones; most impressed by Peter's ruby shoes.. they glitter!

So heres a little list of links for the day:

Command line tricks: Scripting Languages

- 2008-02-14 16:24:27

To search your php.ini file quickly and easily with the option to use regular expressions, I tend to drop back to the cli. The reason for this is I can easily parse the output of phpinfo() with grep, and can do various things with the output, could even pass it to a script if I really wanted to.

Here is the line I use to search phpinfo()

echo "<?php phpinfo() ?>" | php | grep -i $search_string

It passes the string through the php interpreter and then searches through it with grep.

You can also do other nifty things with the shell & php + ruby especially (though I imagine python & perl work in the same way.) For instance I wanted to see if the following ruby would return the number of seconds since the epoch till now.

puts Time.now.to_i

Now I could fire up a PHP page and do something like the following

<?php
    echo "php: " . time() . "\n";
    echo `ruby -e 'puts "ruby: #{Time.now.to_i}"'`;
?>

But what if I've not got a web server with PHP running on the machine I'm using? Well then I could drop back to the shell and run it through php using cat as a way to insert multiple lines, and it would look like the following

Julius:~ caius$ cat | php
<?php
    echo "php: " . time() . "\n";
    echo `ruby -e 'puts "ruby: #{Time.now.to_i}"'`;
?>
^D
php: 1203004463
ruby: 1203004463

Now this works, but why do I want to remember all that php, and seeing as I have to drop back to the shell to access the ruby statement, why not just let the shell do all the work? So after a few seconds thinking, I came up with this

ruby -e 'puts "ruby: #{Time.now.to_i}"' && \
echo '<?php echo "PHP: " . time() . "\n" ?>' | php

This runs the ruby code through ruby and the php code through php without dropping back to the shell from within a language interpreter :)

Update:

Following fangels comment pointing out php -r is the equivilent of ruby -e I've since modified the final commands to read as follows

ruby -e 'puts "ruby: #{Time.now.to_i}"' && \
php -r 'echo "PHP: ".time()."\n";'