flwyd: (dogcow moof!)
I've spent much of today skimming really boring job postings. There's a certain uniformity in job board language that makes reading about exciting work really droll. This means (unless you're picky about domain like I am), it's hard for any job posting to stick out. So I was amused to find this job posting written in Ruby on the Joel on Software jobs board. I could tell the company was at least kinda hip when I saw their name was Shop It To Me. It's exactly the sort of character I would dig in a company; unfortunately they're working in a domain I'm not interested in.

Ruby Soup

Thursday, November 20th, 2008 12:06 am
flwyd: (java logo)
I had a thought at work the other day. Java has a strict one-parent-class rule. Philosophically, this is a good match in domains like taxonomic biology (a phylum is in one kingdom, a genus is in one family). But it's a terrible match in domains like cooking. A soup inherits flavors from many sources. You can add meat to what was originally a vegetable stew. Ruby is a much better language for programming a curry.

goldberg.rb

Monday, October 22nd, 2007 08:57 pm
flwyd: (what would escher do)
Someone should organize a Ruby Goldberg competition in which programmers come up with convoluted-yet-interesting scripts to perform relatively simple tasks.


(Trust nerds to think alike: there's already a Goldberg Ruby on Rails project. But it doesn't look sufficiently absurd.)

++preincrement

Sunday, March 18th, 2007 11:08 pm
flwyd: (mathnet - to cogitate and to solve)
A lot of programming languages, going back to at least C, support preincrement/postincrement operators ++ and --. For instance (in perl, with \n ignored for clarity):
$i = 42
print ++$i

43
print $i++
43
print $i++
44

Explicitly, the expression ++variable changes variable to be variable + 1. The value of the expression is the new value of variable. On the other hand, the expression variable++ changes variable to be one larger, but the value of the expression is the old value of the variable. In most languages, ++variable is equivalent to variable += 1. 1 receives special treatment because of the sheer volume of C code which accesses successive items in an array or increments or decrements a counter.

Ruby is an interesting language. It's got a lot of flexible syntax, borrowed in large part from perl. But in Ruby, everything* is an object and all operations are method calls on an object. Any class can define a + method, so 'book' + 'keeper' == 'bookkeeper' and matrix notation is easy to handle. Classes cannot define a += operator. Instead, ruby treats i += 42 as shorthand for i = i + 42 and l = [1, 2, 3]; l += [4, 5, 6] produces [1, 2, 3, 4, 5, 6]. But a warning to C, C++, Java, and Perl junkies: ++ and -- do not work the same way:
irb(main):001:0> i = 42
=> 42
irb(main):002:0> --i
=> 42
irb(main):003:0> ++i
=> 42
irb(main):004:0> i++
SyntaxError


Why? - and + are unary operators. -42 is equivalent to 0.-42 (that's invoking the "-" method on 0 with 42 as an argument, possibly clearer as 0.-(42)). +foo is equivalent to 0.+(foo). Furthermore, unary operators can be repeated: -(-42) is the same as --42 is the same as 42 (since there are two negatives). ++foo is the same as ++++++++++foo is the same as +(+(foo)) is the same as foo. Furthermore, whitespace is ignored, so while i++ as the last line of a block is a syntax error, the following may surprise you:
irb(main):001:0> 42++
irb(main):001:0* --42
=> 84


I should also note that Python has the same behavior as Ruby (and had it first). In addition, Python lets a class define its own __iadd__ method which responds to += syntax:
>>> l = [1, 2, 3]
>>> l += [4, 5, 6]
>>> l
[1, 2, 3, 4, 5, 6]
>>> l.__iadd__([7, 8, 9])
[1, 2, 3, 4, 5, 6, 7, 8, 9]


I wrote this post in part because Googling ruby "++" returned lots of pages which mention both Ruby and C++. And since ++ doesn't have any particular significance to the language, none of the documentation returned by ruby "++" -"c++" mentioned it. So by mentioning python, ruby, ++, --, plus plus, minus minus, and operator in close succession I hope to aid future confused language converts. Thanks to #ruby-lang for the quick answer.

I should also mention that ++i in these languages is equivalent to the LISP/Scheme (+ (+ i)). Furthermore, in those languages you can define your own implementation of the ++ or -- function. You can even define a variable i and functions ++i, --i, i++, and i--:
> (define i 42)
> (define ++i (lambda () (set! i (+ i 1)) i))
> i
42
> (++i)
43
> (++i)
44


(Un?)fortunately, that function only affects one variable. (++j) would complain that the function ++j doesn't exist.

*: Not quite everything in Ruby is an object or method call. Control structures like if then else are expressions (unlike languages such as C, Java, Perl, and Python), but they are not method calls; you cannot override the if method (unlike functional languages like LISP and Scheme or pure object-oriented languages like Smalltalk and Io).
flwyd: (hexley fork)
At some point, my iTunes library location was incorrectly set to the Music folder in my home folder instead of my external hard drive. [livejournal.com profile] tamheals downloaded several albums, ripped a few, and we bought six songs from the iTunes store. At a later time, the setting was changed to the correct location on the external hard drive and the files from the Music folder were re-added to iTunes and copied to that path. I therefore had two (in some cases three) entries per song for these albums. One evening I went through and moved the Music files to the trash and deleted them from the iTunes list, making sure I could still play them. A few weeks later, Tam emptied the trash. Suddenly, all of the files I'd deleted were marked as missing from iTunes. Somehow (presumably user error) the files were no longer on the external hard drive.

The iTunes Store (unlike Calabash) only lets you download each purchased song once and advises you that you can burn them to CD. Burning a CD with six unconnected songs hadn't seemed like an important move. This is not a feature which benefits the consumer in any way

Fortunately, most of the deleted files are on Tam's iPod. Unfortunately, the iPod file system is obfuscated. Fortunately, there's a mp3info Ruby gem and I've been reading the Ruby Cookbook. I wrote the following script to go through all the mp3 files on the iPod and print the title, artist, album, track, and file path. I then went through the songs marked missing in iTunes and copied the file names into a text file, munging each line into a copy statement.

The script doesn't work on .m4a (AAC) files since mp3info can't handle them. Several minutes of googling finds only references to an MP4Tag ruby library by Miles Egan. Links to that page produce a 404. Archive.org has the old page, but didn't archive the tarball. The site seems to have been hijacked. If I get ambitious I may try to find an AAC specification and parse the files myself, but I think I'll accomplish the task with a list of songs I'm looking for and grepping each .m4a file.

Update, two hours later: Files purchased from the iTunes Store have a .m4p extension (rather than .m4a), so recovering the MIA purchased songs was a simple as ls -l /Volumes/RhiPod/iTunes_Control/Music/*.m4p and comparing the file modification dates with those iTunes remembered for the missing songs. I also found faad2 and installed it through DarwinPorts. faad2 is a decoder for AAC files and the -i command line option prints a lot of information, including metadata. I ran it on all the .m4a files and saved the output to a textfile which I can process with the flip-flop (..) operator.

findmp3s.rb )
July 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2025

Most Popular Tags

Expand Cut Tags

No cut tags

Subscribe

RSS Atom
Page generated Tuesday, July 8th, 2025 11:26 am
Powered by Dreamwidth Studios