April 26, 2008 4:54pm |
1 Comment
| code, geek, programming and ruby
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.
April 1, 2008 8:27am |
2 Comments
| habari, habaricon, link, social and tech
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!
March 1, 2008 11:34am |
14 Comments
| barcamp, camera, geek, manchester, social, tech and uk
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:
February 14, 2008 4:24pm |
1 Comment
| bash, cli, code, geek, php, programming and ruby
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....
More...
February 12, 2008 12:28pm |
1 Comment
| code, datamapper, geek, merb, programming, ruby and tech
And now an example of how I write my ruby code and get it down to the bare, readable, minimum code needed. This is real life code taken from a website I'm building, but I've changed the objects to a blog post because more people will relate to that easier.
The show object has an id passed in using the params Hash, I want to check if that post exists in the database first. If it does, then render the page, and if it doesn't return a 404 error page.
So I start off by writing this in longhand ruby, I'm using the merb framework with DataMapper ORM by the way....
More...