Ben Sharpe
Developer, Entrepreneur, Husband, Dad, Nomad

I wanted to take a date range and iterate over it to produce results by day, but when I went to check the results, there were results by second.

def my_date_thing(a_range)
  a_range.each do |date|
     # something interesting by date
  end
end
 
my_date_thing(10.days.ago .. 0.days.ago)

Turns out, that by default those cool days.ago methods return DateTime objects not Date objects. DateTime objects iterate by seconds, not by days.

But here’s a quick fix: ensure that the range is based on dates first

def my_date_thing(a_range)
  a_range = (a_range.begin.to_date .. a_range.end.to_date)
  a_range.each do |date|
     # something interesting by date
  end
end
 
my_date_thing(10.days.ago .. 0.days.ago)

Now, the range a_range will always iterate by day.

This photo is available in my Instacanvas gallery at: http://instacanv.as/bsharpe

I have loved green-fielding apps in Rails for years. I think my first Rails app, a timesheet for my wife’s employees, was in sub 1.0 Rails. I ended up migrating that app through every major version of Rails up to 3.0. Then, she sold her business.

I used to work for a really great consultancy in Seattle, called Ubermind. I was one of two Rails specialists in a sea of iOS devs. It was so much fun to crank our api’s or backend services for client’s iOS apps. My longest term project was a legacy-maintenance gig that eventually turned into a very successful rewrite. Ah, the good times.

Along the way I’ve learned a few tricks of the trade when dealing with legacy apps or migrating to new Rails versions. I hope to add a few of those tips here.

One of the most common mistakes in programming is typos.   Your brain wants to type one thing and your fingers another.

In Corona (and Lua in general), variables aren’t declared explicitly before you use them.  When  Lua sees a variable name that you haven’t declared as local, it starts looking up the chain of scopes until it reaches the Global namespace (_G).  If it gets there, it creates a key in the _G table with your variable name.

This little trick (shown to me by my friend Chris Blackwell) makes Corona tell you whenever this happens.  Sometimes it’s fine, but most of the time you’ll find a typo (as I did in the Particle Candy library).   I even found that loading the built-in JSON library causes it to check for a global called “Chipmunk.”  Granted, Chipmunk isn’t a typo, but it is accessing a variable that doesn’t exist.  This can happen if you’re check to see if something has been loaded or not.  In those cases this warning can be ignored.

So, put this in your app’s main.lua, run your app in the simulator and watch the console output.  You might just find a problem you didn’t know you had.

Note: You only want this in debug builds.  You could wrap it with a check for running on the simulator, but the best bet is to just remove it before building your final version.

local globalsmeta = {
__index = function(table, key)
print("!!! access to nonexistent global variable "..tostring(key).."n"..debug.traceback())
end
}
setmetatable(_G, globalsmeta)

This works by adding a metatable to the Global namespace to call this function whenever you access an unknown key in its table.

 

[Update May 15, 2013 –  Fixed launcher for osx and windows]

I’ve recently been trying out different editors as I can see the end of the road for TextMate from where I’m sitting.   I really wanted to be super-awesome and finally learn vi or emacs, but despite several attempts, my brain hasn’t really latched onto them like it did when TM came onto the scene.

Now there’s SublimeText 2 (in beta) which is starting to be the editor that TM wanted to be when it grew up.  There’s still a lot of funky things with it, but I can see the potential.  Not to mention it’s cross platform like the ancient gods vi and emacs.   I suppose that’s really just icing on the cake because I rarely leave my home platform for reasons other than duress (Windows) or curiosity (Linux).

The one thing I really missed from TM was the ability to launch the CoronaSDK simulator from the editor.   It’s fantastic to be able to edit a file and hit a button and have it fire up the simulator and then start reloading the simulator each time the file changes. (actually the simulator does that work for you)  I took a look at the Corona bundle for TM and found quite a few lines of AppleScript that are doing the work (i kid not).

tl;dr

In Sublime, select “Tools > Build System > New Build System…”

This opens up a new text file.  Copy and paste this into it:

{
"osx" : {
"cmd": ["/Applications/CoronaSDK/Corona Simulator.app/Contents/MacOS/Corona Simulator","main.lua"]
},
"windows" : {
"cmd": ["C:\Program Files\Corona Labs\Corona SDK\Corona Simulator.exe","main.lua"]
},
"file_regex": "^[ ]*File "(...*?)", line ([0-9]*)",
"working_dir": "${project_path:${folder}}",
"selector": "source.lua"
}

By setting the working_dir to the project folder and specifying ‘main.lua’ as the file, you can launch the simulator for a project no matter what file you happen to be looking at (or in what subfolder).

Save the file as “CoronaSDK.sublime-build”

  1. Go to your Corona project
  2. Open a file to edit
  3. Select  Tools > Build System > CoronaSDK
  4. Press Command-B
  5. profit!

For Reference,  custom build system files are usually stored in the following locations:

OS X: ~/Library/Application Support/Sublime Text 2/Packages/User

Windows: %APPDATA%/Sublime Text 2/Packages/User

Amazon's Kindle Fire
Amazon's Kindle Fire

  1. Build the app using  File > Build > Amazon/Kindle…
  2. On the Fire, go to Settings and tap More
  3. Tap Device
  4. Turn ON “Allow Installation of Applications — from unknown sources”
  5. Plug in a Micro-B usb cable into the Fire and the other into your computer (in this case a MacBook Air)
  6. The Kindle should be showing up on your desktop
  7. Copy the .apk file generated by Corona onto the Kindle (I’m not sure that it makes a difference where — I used the “downloads” folder)
  8. Eject the Kindle from the Mac
  9. On the Kindle you’ll need a File management application installed (I used “ES File Explorer” because it was free and had the most reviews)
  10. Use the File manager to find your app and tap it to install it
  11. Profit!

Update: Dejay was correct, you have to try and open the file to know for sure if it’s there or not.

Want to know if a file exists before you use it?

Lately, I’ve been wanting something a little more dynamic in my projects for opening various Scenes (think Director, but not).   I needed a way of looking to see if a file existed or not before I tried to load it up.  Here’s the result:

function fileExists(fileName, base)
  assert(fileName, "fileName is missing")
  local base = base or system.ResourceDirectory
  local filePath = system.pathForFile( fileName, base )
  local exists = false
 
  if (filePath) then -- file may exist. won't know until you open it
    local fileHandle = io.open( filePath, "r" )
    if (fileHandle) then -- nil if no file found
      exists = true
      io.close(fileHandle)
    end
  end
 
  return(exists)
end

Typical usage:

if fileExists("myGame.lua") then
  -- do something wonderful
end

If the file does not exist, the CoronaSDK library function system.pathForFile() returns nil.  It also returns a Warning in the console, which can be safely ignored.

By default it checks your app’s asset directory (system.ResourceDirectory), however, if you’d like to check if a file you created exists you can pass in an alternative base path like so:

if fileExists("some_file.txt", system.DocumentsDirectory) then
  -- do something wonderful
end

The three valid options for base path are:

  1. system.ResourceDirectory
  2. system.DocumentsDirectory
  3. system.TemporaryDirectory

Update April 2011: I now use the Heroku Plus gem to manage these accounts.

Read more →

Getting your development environment setup just the way you like it takes some time.   I’ve done it several times recently and this is the setup I feel most productive with.
Read more →