Ruby
Tweeting New Jekyll Posts From Github Actions - Part 2
I previously wrote about my experience attempting to use Github Actions to post a tweet every time I publish a new post on my self-hosted Jekyll/S3/Cloudfront blog. I managed to get to a working solution that was too complicated, so I’m trying another approach. I was following this post by Dave Brock where he described using the commit message as the entire tweet - so every commit message and git push is a tweet. I dismissed it as too simple, but now that I’ve seen how complicated the alternative is I’m going to try something similar.
Dynamic Programming Algorithm Question
I recently watched this thoroughly entertaining video by ThePrimeagen (aka Michael Paulson) where he pretended to solve algorithm questions on Leetcode as a job interview. He solved 6 problems in incremental difficulty, and when he got to the final problem I paused the video and tried to solve it myself.
Overriding Named Route Parameters in Rails
Rails will use :id
as the default parameter in resourceful routes.
Ruby Hash
One of the popular feature’s of Ruby’s Hash object is that you can specify a default value for entries in the hash. For example, a common use case is to count the frequency of objects in an array.
Faster CSV Imports with Rails
On my current project we deal with quite a few CSV imports. While writing a basic CSV import is reasonably straightforward, we ran into performance issues when we started scaling the solution. Basically we could import 3,000 users in around 30 minutes (the domain model being rather complicated), but we needed to scale this solution to import 180,000 users every night. It was obvious that we needed to put some effort into the performance of our solution.
Using Pluralize in your Models in Rails
One of the really nice helpers in Rails is pluralize. It basically provides a really simple way of finding the plural form of a word, courtesy of ActionView::Helpers::TextHelper.
Rails File Size methods on Fixnum
As a Rails developer I have often seen the shorthands for specifying different dates using the built-in methods on Fixnum. For example, if you want to specify the date for 5 days ago, you can simply use:
Convert seconds to hours:minutes:seconds in Ruby
Today I was working on an analytics report where the time a user spent on the site was being reported in seconds. I wanted to convert this into a nicer format – hours:minutes:seconds. I had hoped that there was a nice helper in Rails to accomplish this, but unfortunately there isn’t.
Allow Users to Remove Uploaded Images with Paperclip
Allowing users to upload images is pretty straightforward in Rails – especially if you’re using the excellent Paperclip gem. I’ve blogged before about how easy it is to configure Paperclip to upload images to S3. However, there are no nice examples (that I could find) of allowing your users to remove images that are already uploaded.
Tips for Implementing Emails in Rails
Sending emails from your Rails application is a pretty common requirement. User signup, forgotten password, order creation, nightly reports – they all require emails. This is pretty straightforward in Rails – we already have the concept of mailers (with views) – you simply need to configure your mail service and you’re done!
RSpec Best Practices
I’ve been using RSpec on and off for the past 2 years. I’ve learnt that it’s easy to write tests that are bloated, slow and don’t offer any value. I’ve also learnt a few tricks and best practices that will make your RSpec life a bit easier.
Simplify your Ruby code with tap
I only recently discovered the tap method, which was added in Ruby 1.9. Basically, if you ever find yourself writing code like this:
Integrating Solr Search with Spree
For the past 2 months I’ve been working on an e-commerce website where we’ve been using Spree. The built-in searching with Spree is pretty decent and simply builds SQL queries to execute against the database. While this is a great starting point we quickly got to a point where we needed to do more advanced searching – for example, we wanted the user to be able to filter by size, which is not something you can really accomplish with basic SQL queries.
Using Rails Helpers inside a Controller
Rails helpers are designed to be used inside views, but sometimes you might want the functionality from a helper inside your controller. For example, you might be generating a simple JSON string and you want to use a view helper to format some element of the JSON.
Could not verify the SSL certificate for RubyGems
Today I ran into the following problem when running bundle install.
Intercept Emails in Rails
On any project where emails get sent automatically testing can become a problem. Ideally you want to be able to see the emails that get generated, but avoid sending test emails to real services or users. On the other hand, you still need to send emails to the real services and users in your production environment.
Enabling Basic HTTP Auth on Rails
On my current project we are hosting our Rails application on Heroku. While this is great in allowing our client to see changes almost immediately, it also means we have a public site which could in theory be accessed by anyone with the correct URL. To alleviate any concerns around this we decided to simply add basic HTTP authentication to the site as a temporary stopgap.
Populating form values through a link in Rails
Today I was asked to help with creating a link on a website that should take the user to a different page where a form should be filled out based on paramaters supplied through the link URL.
How much duplication should we tolerate in tests?
As a developer I’m used to avoiding duplication in code. Avoiding duplication is even listed as one of the 4 rules of the TDD cycle:
Debugging in Ruby
Before I started doing Ruby I was doing mostly C#. This meant I did pretty much all my development in Visual Studio, giving me access to a very powerful debugger. Coming into the world of Ruby meant switching to Vim, which meant my editor started up in less than a second (instead of a few minutes), but it also meant I had to give up all the debugging power.
[BUG] cross-thread violation on rb_gc()
I was recently involved in upgrading a project from Ruby 1.9.2 to 1.9.3. As part of this upgrade we also upgraded several of our gems (including Rails) to the latest versions. Everything went smoothly, but when I tried to run the latest version of the code on my laptop (MacBook Pro) I got the following error:
Comment Ruby code with Vim
I’ve recently started using Vim for Ruby/Rails development. As someone who is very new to Vim it has been a steep learning curve, but I’m really enjoying it – it has definitely become my editor of choice. One of the useful little tricks I figured out today is how to comment out a block of Ruby code.
Closures in Ruby
The complete title for this post is: Closures in Ruby make my head hurt. I’ve had a rough idea of how closures work (from my JavaScript experience), but I just watched An Introduction Blocks, Lambdas and Closures in Ruby and I’ve realized that I my knowledge is a mere drop in the ocean.
Alias class methods in Ruby
Yesterday I came across a problem where one possible solution was to alias a class method. I’ve blogged about aliasing methods before, but I didn’t know how to do this for class methods.
Using Zip in Ruby
Last week I blogged about the Enumerable module in Ruby and specifically mentioned the Enumerable#zip method. I didn’t think the zip method was particularly useful, but I have actually found a few extra features which are very nice.
Presence of a substring in Ruby
I just watched the ‘Ruby Trick Shots’ video. This video basically shows a bunch of very useful methods, libraries and techniques you might be unaware of. If you’re a Ruby developer I would highly recommend it.
For vs Each in Ruby
One of the discussions I was involved in at RubyFuza revolved around the difference between for and each in Ruby. There is only a subtle difference around scoping, but I think it’s an important distinction because it reveals some important aspects of Ruby.
Exploring Enumerable in Ruby
One of talks I was able to attend at last week’s RubyFuza conference was by Andrew Timberlake titled ‘Ruby’s Hidden Gems’. It basically revolved around examples of how we often implement something in Ruby that is already supported in the API. I’ve blogged about this before – how I found myself writing C# code, but doing it in Ruby.
RubyFuza 2012
Last week I was lucky enough to be able to attend the RubyFuza conference in Cape Town, South Africa. As far as I know this is the biggest Ruby conference in South Africa and when ThoughtWorks offered me the chance to attend I grabbed it with both hands (even though it’s a long flight from New York).
Checking if variables are defined in Rails Partial Views
During this week I was working on a Rails app when I ran into an issue where I needed to check if a variable has been defined in my view. The standard way to check if a variable is defined in Ruby is with the defined? operator.
Book Review: Programming Ruby
TLDR Version: This book is incredibly well researched and covers pretty much everything in the Ruby world. While it does have a very good introductory section I think the main benefit to this book is as a reference guide.
Method Resolution in Ruby
While reading the Ruby Pickaxe book I realized that while JavaScript and Ruby are both dynamic languages, they handle method resolution in very different ways. I often use JavaScript as a reference since it’s the dynamic language I’m most familiar with.
Test Behavior, Not Implementation
While I was reading the Ruby Pickaxe book I came across an example of Unit Testing which – in my opinion – is testing implementation rather than behavior. This particular example was used to explain Duck Typing in Ruby, but I think it illustrates that it’s pretty easy to write tests which end up testing the wrong thing.
Catch and Throw in Ruby
In my last post I had a look at handling exceptions in Ruby and the functionality around that. In this post I’m going to look at how Ruby also allows us to unwind a single block of code without raising an exception. I’ve said before that exceptions should be exceptional – the functionality in Ruby offers a lightweight version of this within a single scope.
Exceptions in Ruby
I like Exceptions. They’re a very useful concept for raising and handling errors at the correct level without cluttering our code. Coming from C# the way Exceptions work in Ruby are intuitive to me, but there are actually a few extra features which aren’t available in the .NET world.
Block Variable Scope in Ruby
I was doing some reading on Ruby and came across some interesting details regarding how block scoping works. Most of it is pretty intuitive – especially if you’re used to scoping in JavaScript – but there were one or two features which I haven’t come across before.
Splat Operator in Ruby
In one of my recent posts someone pointed out that using the splat (*) operator would have made some of the code a bit simpler. I’ve seen the splat operator being used in method definitions (to group remaining arguments), but it turns out there are actually quite a few other uses as well.
Symbol#to_proc in Ruby
In the reading I’ve been doing around Ruby I tend to come across quite a few snippets which I don’t fully understand. The following is a line from ‘The Rails 3 Way’:
Alias methods in Ruby
I came across an interesting case where using Ruby’s method aliasing was really useful. If you’re not familiar with the alias functionality, the following example should explain it.
Ruby map, collect and select
As a newcomer to Ruby, I have often been confused by the 3 very similar enumerators on the Array class: map, collect and select. Let’s try to figure this out using an example.
Build a Collection of Unique Values in Ruby
One of the really cool things about working with Rails is that you can actually look at the source code – it’s a bit like seeing how a magician does all his tricks. I’m always interested in this type of code since it’s usually a very good indication of best practices.
First steps with Ruby – from a C# guy
As it seems very likely that my first ThoughtWorks project will be a Ruby project, I have been spending as much time as possible getting up to speed. After getting HomeBrew and the latest Ruby version installed (this was more painful than I would care to admit) I worked through the first 25 problems on Project Euler. This really is an excellent way to practice using a new language – plus it’s quite fun.
Ruby: Summing values in an Array
In my last blog post I illustrated a very simple usage of Ruby’s inject method (more specifically, Enumerable#inject) – summing the values in array. In the comments Wayne Conrad pointed out that there is an even simpler way of doing this and it got me thinking – how many different ways can you sum the values in an array?
Using Ruby Inject
As I mentioned in my last post, I’m running through the problems in Project Euler to learn Ruby. It’s a really fun way of learning a new language while solving interesting problems.
How I’m learning Ruby
Ruby is a tough language to learn for a C# developer. There, I said it. There are quite a few fantastic books out there that will really help you learn this awesome language, but it’s still a difficult language to learn. If you’re used to having IntelliSense and using Resharper all day it’s a difficult adjustment to make.