• Set Timezone from Terminal OSX

    I often have to switch between timezones to test our timezone-sensitive application code. I was getting annoyed at having to open the settings screen in preferences (which is slow) and found out how to do it from the command line.

    To set your timezone run:

    sudo systemsetup -settimezone timezone

    Where timezone is a valid zone from this list:

    sudo systemsetup -listtimezones

    Finally, you can get your current system timezone using:

    sudo systemsetup -gettimezone

    This command can easily be made into an alias like so:

    settz="sudo systemsetup -settimezone $@"

    So all you need to do to change your timezone is settz GMT!

  • Invalid Byte Sequence in US-ASCII

    After some new code was checked in at work we encountered this issue in our CI as part of the build step to run RubyCritic over our code. I’d never seen it before, and the source of the error was in buffer.rb of the parser gem library:

    'source=': invalid byte sequence in US-ASCII (EncodingError)

    I did some digging and I found that this is where RubyCritic parses each file into an abstract syntax tree for analysis. It seemed like there was a character in the file that could not be parsed correctly, and eventually I found a StackOverflow post that pointed to a tool called iconv that can be used to convert between different character encodings, and that if a conversion is unsuccessful it will throw an error and return code 1. Now this was all well and good but the error I was getting from buffer.rb did not tell me the currently erroring file – the best I could do was modify my local gem source to give me a list of the files that passed through the RubyCritic library for analysis.

    Then, now that I had a list of files, I could run each file through iconv to check which one had invalid ASCII characters. Of course I am a programmer and thus lazy so I wasn’t going to sit there and run it manually on every damn file, so I just made a ruby script to run it on each file in my list (of which there were hundreds):

    def run 
      SOURCE_FILES.each do |file|
        file_path = SOURCE_DIR # source dir is the full path of the root directory
        puts file_path
        puts `iconv -f us-ascii #{file_path} > /dev/null; echo $`

    I ran the script and it found the file easily by finding the one that returned 1. Then, all I did to fix the issue was delete the code that had been changed in the previous commit, re-typed it manually, then saved the file. I ran my script again and the issue was solved!

  • Expanded Output Format for PSQL

    If you are using psql you may be getting annoyed that your query results look like this for tables with more than one or two columns:

    default display psql

    Well, there is an answer to this problem. Just enter the command \x on and you will turn on the expanded display option, which makes your query results look like this:

    expanded display psql

    Much better!

  • PhantomJS Element Not Found Using Capybara

    I ran into an odd issue this week where one of our Rails feature specs using Capybara was failing because the PhantomJS driver (driven by Poltergeist) couldn’t find an element on the page. This was strange because the same element was tested on a different page and PhantomJS could find it just fine. This occurred even with $(document).ready(). This is kind of the layout of the page:

    <div id="element-to-find"></div>

    And the JS we were trying to run:

    $(document).ready(function () {

    Our specs were failing with a JS error saying that element element-to-find could not be found. We messed around a bit with some page.body calls to get the HTML of the page during the testing, and everything seemed to be in order. The only different thing about the view was that it had quite a few nested partials and the JS code was at the bottom level of these partials. On a whim, I changed the HTML to look like this, moving the JS call to the bottom of the page:

    <div id="element-to-find"></div>

    …and it worked! It is, of course, best practice to load JavaScript at the bottom of each page, so I guess we had gotten lucky so far with how quickly the HTML was loading. Though I’m not sure why $(document).ready() didn’t help in this situation. If you have any insight please let me know!

  • Jim Weirich Ruby Talks

    I was linked to these two videos by Jim Weirich when I started working with Ruby and Rails again by my friend/colleague/boss and they really helped get into the mindset of working with Ruby in a good, SOLID OOP way. They also gave me a path to not use Rails magic too often, and to decouple logic and classes from the framework as much as possible.

    Advanced Ruby Class Design by Jim Weirich

    Decoupling Ruby from Rails by Jim Weirich

  • Simulating the Mouse Click Event in JavaScript

    I was trying to write a capybara feature spec for a date input using flatpickr. I wanted to assert that once I selected a date in the picker, it would be successfully entered into the input field in the required format. To do this I wanted to simulate a mouse click event in JavaScript. However, I hit a snag when attempting to fire the click event on a date in the flatpickr calendar instance, which had the CSS class flatpickr-day. When selecting the element and triggering the .click() event, nothing was happening; the date was not selected, the flatpickr instance did not close, and the input remained empty.


  • Git Shortcuts with FZF

    Today I started using FZF, which I’d heard of before but haven’t found useful until now. It is a fuzzy file finder written in Go, which can be used to rapidly locate a file with the arbitrary string provided, starting with the current directory or a provided directory or STDIN.

    Where I have found it most useful so far is alongside git. The first command I’ve set up an alias for is git add. I wanted to be able to add an individual file using git, though I didn’t want to have to type out the full path of the file every time. This command gets all of the added, deleted, and modified files in the current repo and feeds them to fzf, where you can then search for and select the file you want. The selected file is then given to the git add command.


  • Custom Time Formats in Rails

    If you need to set up custom date formats in Rails, for example to show in Views, you can do so by creating a config/initializers/time_formats.rb file and adding as many of the following as you want:

    Time::DATE_FORMATS[:au_datetime] = '%e/%m/%Y %I:%M%P'
    Time::DATE_FORMATS[:au_date] = '%e/%m/%Y'

    You can even use lambdas when defining a format, which will be executed using .call when you call to_s on your Time:

    Time::DATE_FORMATS[:short_ordinal]  = ->(time) { time.strftime("%B #{time.day.ordinalize}") }

    This is covered in more detail in the Rails Time documentation here:


  • Back to Ruby

    After almost two-and-a-half years of the high-paced, glamourous lifestyle of a UI developer (what we definitely need is more frameworks), I’m putting the JavaScript (partially) on the shelf along with the C# and .NET to work once again with Ruby and, for the first time professionally, Rails. I’ve decided to work with an old colleague and friend in a software consulting company, Webbernet, which entails an exciting move from Brisbane to Melbourne. The first half of this year has been and will continue to be an exciting and frantic time, and I am rediscovering what I liked the most about Ruby after my first week on the job, namely its pleasing syntax, great testing story and culture, and frameworks like Ruby on Rails that make it easy to get a functioning application up and running quickly. As a refresher I watched the following videos on Ruby and Rails that I really recommend to anyone wanting to try either or both out:

    From now on expect to see more posts about Ruby and Rails alongside the general programming articles I usually post. I’m even heading to the birthplace of Ruby (Japan) next month, and along with my other huge life changes it’s good to be good to get the fire burning again!

  • Organising C# using Statements in Visual Studio

    If you’ve been working on a C# file for a little while, refactoring as you go, you may notice that some of the using statements are grayed out, which means you are no longer using their assembly code in the current file. Also, they may be out of order alphabetically. Fortunately there is a way to fix both of these issues in Visual Studio. You can do it manually by going to Edit > Intellisense > Organise Usings > Remove and Sort Usings or you can bind this action to a keypress, say Ctrl+U.

    Remove and sort usings

    To do this, go to Tools > Options > Keyboard, and inside the Press shortcut keys textbox press the key combination that you want to use. Then type usings in the Show commands containing: textbox. Then choose EditorContextMenus.CodeWindow.OrganizeUsings.RemoveAndSort, and press Assign. All done! I found out how to do this from this StackOverflow post:

    Shortcut to organize C# usings in Visual Studio at StackOverflow

2 // 10



Want to read regular updates? Subscribe via RSS!