https://alpha.app.net/riyad/post/10856992
Tag: Source Code
API Response Fail
If you must use XML in your APIs … OK, I can handle that. But if your responses look like this … I hate you!
WHY?!?!
Convert any file VLC can play to mp3
I just felt the need for a script that could extract the audio track of a video, transcode it and save it as an mp3 file … 2 hours later I was finished (get the Gist). 😀 It uses VLC to do hard work. 😉
Thanks to Kris Hom for the inspiration. 🙂
Update 2014-03-01:
- Check whether VLC is installed
- Should also work on Linux now
- Increase default bit rate to 192kbit/s
- Fixed bug where the file/playlist would repeat endlessly
Update 2014-11-05:
- Also look for VLC in “~/Application/”
Update 2016-03-05
- Added a ffmpeg version
The Y Combinator
I take my head off to Jim, that’s a great way to approach a weird intersection of mathematics and programming. 😉 For those who are curious … he uses a very simple mathematical algorithm to explore how you can express recursions in Lambda calculus and thus “derives” the Y combinator.
Totally useless, but worth every minute. 😉
Default Values for Boolean Options in Ruby
Let’s say you read settings from a YAML file and have some sort of settings object. Then you check if certain options are set with custom values or you have to set default/fall-back values on them. If you are dealing with Boolean options you have to be careful … as I had to find out myself.
Initially you would probably do something like the following to set a default value on a Boolean option:
settings[:some_option] ||= true # set default value if nothing set
Do you see the problem? What happens if the option was deliberately set to
false
? You would overwrite it because both cases
nil
(i.e. nothing set) and
false
would evaluate to
false
in the context of the
||=
operator and you would in both cases assign the right hand value (and overriding an explicit user choice in one case) … *ouch*.
So the correct solution is something like the following:
settings[:some_option] = true if settings[:some_option].nil?
Just be careful … 😀
TextMate 2 on Github
woot? o_O Macromates released the source code for TextMate 2 on GitHub. 😀
Old Games’ Source Code
Fabien Sanglard has written an interesting drill down into the recently released source code for Doom3. He tries to reason why certain things are the way they are and also had a few of his questions answered by John Carmack himself.
There was news about the source code for the Prince of Persia game on Apple II computers restored from a set of ancient floppy disks. There is also a great quote on why going through the trouble of restoring/releasing the source code of those games is something worthwhile.
“Because if we didn’t, it might have disappeared forever.”
Video game source code is a bit like the sheet music to a piano sonata that’s already been performed and recorded. One might reasonably ask: If you have the recording, what do you need the sheet music for?
You don’t, if all you want is to listen and enjoy the music. But to a pianist performing the piece, or a composer who wants to study it or arrange it for different instruments, the original score is valuable.
It’s possible, up to a point, to reverse-engineer new source code from a published video game, much as a capable musician can transcribe a musical score from listening to a performance. But in both cases, there’s no substitute for the original document as a direct line to the creator’s intentions and work process. As such, it has both practical and historical value, to the small subset of the game-playing/music-listening community that cares.
Render Rails assets to string
If you ever needed a way to render a Rails assets to a string, Hongli Lai from Phusion describes how. 🙂
I prepared a Gist wrapping it into a nice helper. 😀
module ApplicationHelper # thanks to http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/ # you may need to change the owner of the tmp/cache/* directories to the web servers user # e.g. for Debian systems: `chown -R www-data:www-data tmp/cache/*` def render_asset(asset) Conferator::Application.assets.find_asset(asset).body.html_safe end end
File.basename Tricks
Just realized Ruby’s File.basename can also filter out (arbitrary) file extensions. 😀
File.basename("foo/bar/baz.html", ".*") #=> "baz"
Problem with escape_javascript and haml-rendered textarea in Rails 3.2.3
Hunting down and fixing this bug took half of my day … 🙁