This is a very small tip aimed for people to clean up their code-base and remove some of the double negatives.
Often I shall see:
unless @user.nil?
The issue with this is approach is that unless is harder to read than if, then you have to mentally process the #nil?
The other approach is:
if @user
However, for a lot of people this is not explicit enough. I find that too few know about #present? method
if @user.present?
#present? is literally the opposite of #blank? and thus checks if it is not nil? or if it is not empty?
Hopefully with this small tidbit of knowledge, you can clean up your code and make it easier to read.
I’ve had a lot of issues trying to deploy ThinkingSphinx with Capistrano. This has been for a multitude of reasons. The following is my recipe for deploying and hopefully making it public will make a few peoples lives easier.
This assumes you are using the plugin, not the gem.
In your deploy.rb, add the following at the top:
require 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx/deploy/capistrano'
Then, at the bottom add this:
before "deploy:rollback:revision", "thinking_sphinx:stop_gracefully"
after "deploy:default", "thinking_sphinx:symlink"
after "deploy:default", "thinking_sphinx:ready"
namespace :thinking_sphinx do
task :stop_gracefully do
begin
thinking_sphinx.stop
rescue
puts "ThinkingSphinx is not running. No stop required."
end
end
desc "Configure, Index and then run Thinking Sphinx"
task :ready do
thinking_sphinx.configure
thinking_sphinx.index
thinking_sphinx.start
end
desc "Link up Sphinx's indexes."
task :symlink do
run "ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx"
end
end
Now this may seem vulagrly blatent, but I have been mulling over past jobs the last few days. I realised that I did myself no favours by being constantly belevonant. When I was asked to “Complete a design in 2 days” or such, I would say jump at the oppurtunity to prove myself or to try and help out in any way I can. Now I realised, by rushing the design work, my performance was lackluster and the results were not of that I was proud of. I was proud that I got the work out, but it was forgettable and obviously, you don’t ever want your work to ever be forgettable.
If your boss ever says “Can you do me a design in 3 days”, mostly I think the correct answer should be “No”, unless it’s a once off request. To be utilised as an average designer, will earn you that position and make you despensable, leaving little reward. It’s a mistake I shall try not to make again in the future, to only put out my best work and not accept putting out anything but that.
I have created a new plugin for jQuery that allows you to animate the text in an element with linethrough.
I’m about to do some work on a Rails project that requires the environment to be that of a real production environment for users. I have created 2 bash functions to aid me in this;
I put them in my bash profile. This is my first foray into bash programming, so if you think something could be done better, please tell me.
In this tutorial I will teach you how to display your last listened to track on your website with the jQuery library (although it can be easily translated to any other library). The JSON library for Last.fm is not very well documented.
-
Log into the API Account page and fill in the form for your application however you would like. Name the Callback URL ‘LastPlayed’. Write down your API key.
-
Create an element somewhere in your page with the id of #song. I use the following:
<strong>Currently listening to:</strong> <span id="song">Nothing</span>
That way, for the few seconds that the Javascript has not loaded, it will appear sensicle.
- Right before the
</head> tag on your website, add the following snippet of code:
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
</script>
<script type="text/javascript">
function show_last_track(JSONdata) {
var last_track = JSONdata.recenttracks.track;
var track_artist = last_track.artist['#text'];
var track_name = last_track.name;
var track = track_artist + " - " + track_name;
$('#song').text(track);
}
</script>
- Finally, add the following code just above the
</body> so that once your page is loaded, it will execute. Don’t forget to replace YOUR_API_KEY with the one you jotted down earleir.
<script type="text/javascript" src="http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=piemaster&api_key=YOUR_API_KEY&limit=1&format=json&callback=show_last_track"></script>
- Reload your page and check it works!
After some ‘complications’ with MediaTemple which ended up with them deleting my data, including all my posts from 4+ years, I have now switched over to a hosted soloution for my blog.
I am going to try and post a lot more than I did, through here.