• 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 $`
      end
    end
    

    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!

  • Patreon

    Okay, I’ve finaly gone and done it and made a Patreon. My motivations for doing so are outlined on the page, but here is the main thrust of it:

    So why make a Patreon if I’m happy to amble along penniless on this writing hobby in my spare time? The answer is you. I want to hear feedback from other people about my writing, what do you like, what do you hate, what do you want to see more of? I want to release my short stories and have them read and adored and/or reviled, and I want to connect with other writers. Most of all I want to know that someone is out there reading my writing, and that will help me get me off my arse to write instead of browsing reddit.

    Rest assured that even if I don’t make a penny from Patreon I will still post my writing online. Thank you for reading this not-oft-updated blog for all these years. It’s been five years since I started this blog, and I don’t plan on stopping any time soon!

  • 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:

    <script>
      ViewJS.initialise();
    </script>
    
    <div id="element-to-find"></div>
    

    And the JS we were trying to run:

    $(document).ready(function () {
      document.getElementById('element-to-find');
    });
    

    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>
    
    <script>
      ViewJS.initialise();
    </script>
    

    …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.

    read more...

  • 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.

    read more...

  • 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:

    http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s

  • 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

1 // 9

 

 

Want to read regular updates? Subscribe via RSS!