Renaming Photos

Let’s do something more useful now. A while back, my wife wanted a program to download the photos from her camera’s memory card and rename them. Let’s build something like that program.

We’ll need a few more of Ruby’s built-in methods to accomplish this. The first is the Dir[] method. We’ve seen [] used with arrays before—you did know that was a method, didn’t you? Yep, it sure is. You say “arr[2]” and I say “arr.[](2)”—it’s all the same.

Anyway, rather than using an array’s [] method, we’re using the object Dir’s [] method. (Dir is for directory.) And instead of passing in a number, as with arrays, this time you pass in a string. This isn’t just any string; it’s a string describing which filenames you’re looking for. It ...

Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.