We do a LOT of file processing, bringing in customer data for analytics, so we’re always looking for ways to make things faster. The CSV class built into Ruby is great, but we found that if we could stand a few less options, we could make it a good bit faster while staying in Ruby. Assumptions: CSV file has a header You want a hash emitted for each row - (headers are converted to symbols) You don’t need converters - all values are strings Most rows in the CSV file do not have quotes What does it do?
Writing data to a file > echo "hello" > myfile.txt > Permission denied Simple enough, sudo to the rescue! > sudo echo "hello" > myfile.txt > Permission denied The sudo applies to the echo command, instead of the “>” operator The fix? Use tee - tee reads from STDIN and writes to the file and to STDOUT - but more importantly for our case, it lets us put the sudo
Like any Rails application, we write quite a few rake tasks to automate activities. Rake is great, but I’ve always been frustrated with the logging. We would use puts in the rake tasks, but other components that used Rails.logger wouldn’t show up in the console - they were going to [environment].log. So I created a simple rake task to switch the logging to STDOUT: #Sets up logging - should only be called from other rake tasks task setup_logger: :environment do logger = Logger.new(STDOUT) logger.level = Logger::INFO Rails.logger = logger end Because this picks up our typical dependency - environment - I can replace environment with setup_logger: desc <<-DESC Creates a job: outreach:jobs:create[<job>,<org>,<args>] Ex: outreach:jobs:create[distribute,community,job_name:close_cases,immediate:true] DESC task :create, [:job_name, :org_short_name] => :setuplogger do |, args| JobLauncher.launch args[:job_name], args[:org_short_name], parse_extras(args) end This seems like an obvious fix, but I haven’t seen it used before.
Recently we were struggling with some performance issues - importing data was taking way longer than it should. Finding the problem Ruby profiler to the rescue! This gem represents everything I love about the Ruby community. Its open source, its intuitive, and it simply just works so I can back to building applications. Profiler setup The simplest setup to test specific code, comes straight from their README: require ‘ruby-prof’ # Profile the code result = RubyProf.profile do …
Sensitive Data in MongoDB At KoanHealth, we work with healthcare data and use Ruby and MongoDB as our primary database to store patient data. This means that is has to be secure at rest and over the network. We looked at a few gems for storing encrypted data, but they: Weren’t specific to MongoDB, and felt clunky Made transitioning between raw and encrypted data difficult Existing solutions There are some solutions, but we really just needed a gem that: Encrypts data at a field level Makes it easy to access the raw and encrypted value Allows developers to use their existing encryption gem Encrypted fields So we’ve built mongoid-encrypted-fields.
Knockout comes with a binding to show elements (visible), but not one to hide elements. So here it is: ko.bindingHandlers[‘hidden’] = update: (element, valueAccessor) -> value = ko.utils.unwrapObservable(valueAccessor()) isCurrentlyVisible = (element.style.display != "none") if (value && isCurrentlyVisible) element.style.display = "none" else if (!value && !isCurrentlyVisible) element.style.display = ""
Simple, but useful shortcuts in a bash shell. Not a complete list, but I find myself using these the most: Editing CTRL + a: Beginning of line CTRL + e: End of line CTRL + w: Delete current word ALT + b: Backwards one word ALT + f: Forward one word CTRL + l: Clear screen See this post to alter your Terminal to use CTRL + Left Arrow or CTRL + Right Arrow for moving between words on a Mac.
Pow!, created by the 37 Signals team, is a “zero-config Rack server for Mac OS X”. If you’re working on multiple applications that are connected, it’s a great tool to keep all of your sites up and running on your machine. Homebrew, “the missing package manager for OS X” makes installing applications simple. Most of the time it just works, and when extra steps are needed, Homebrew usually explains what you need to do once the install is complete.