Tagged Ruby


When you have a number that you have to display that’s greater than 10,000, it’s really tempting to hard-code the quantifier so you can shorten the number. Something like `12.6 Thousand`.

That’s all well and good, but we like numbers to grow. What happens when you hit 1 million? You don’t want 1000.1 Thousand showing up on your site. That’s no fun.

This is where Number_to_Human comes to the rescue. It pretty prints a number in a way that is more readable by us humans. For example, 12790000000 becomes 127.9 Million. And the best part? It will automatically switch over to 1.0 Billion when the time is right.

number_to_human(number, options = {})

This helpful helper method gives you lots of customizability too. Things like :precision, :separator, :delimiter, and :units can all be set by passing in custom values in the options hash.

It kinda just works how you would think too. Which is great.

number_to_human(123) # => "123"
number_to_human(1234) # => "1.23 Thousand"
number_to_human(12345) # => "12.3 Thousand"
number_to_human(1234567) # => "1.23 Million
number_to_human(1234567, precision: 1,
                         separator: ',',
                         significant: false) # => "1,2 Million"

Hopefully you can use this to remove hard-coded units from your views.

For full documentation, see the docs.


If you’ve ever wanted to implement a search function in your Ruby on Rails application, you might want to read up on excerpt. Built into ActionView, this helper function lets you extract an excerpt from text that matches a phrase.

This can allow you to show context along with the search results to a user, which is definitely better than just getting the page title.

excerpt(text, phrase, options = {})

excerpt('This is an example', 'an', radius:5)
# => ...s is an exam...

Through use of the provided options, you can control how much is seen on either side of the match.

# break on whitespace between words.
separator: ‘ ‘

# return 8 items on either side of the phrase
radius: 8

Let’s take a look at a more complex example to see how it might be helpful to use for a search results page.

excerpt('Today we had a very beautiful sunrise', 'very', separator: ' ', radius: 2
# => ...had a very beautiful sunrise

As you would expect, the excerpt grabbed the 2 words before and after the matched phrase. Since sunrise is the last word, you don’t see the ellipsis at the end. This gives you the context to know if that result is the one you are really after.

Hopefully you can use this to provide better context for your users.

For full documentation, see the docs.

Omaha Code School Wiki

A week ago we wrapped up our first group project. My team decided to create a Omaha Code School Wiki. It’s a wiki about anything related to Omaha Code School. Anyone can read the articles we put together so far. You have to sign up for an account in order to add or modify the articles, but anyone can do it. Make sure you provide a legitimate email, because we figured out how to send email to confirm before we activate your account. We’re pretty proud of that.

Here is the link for your enjoyment. Let me know what you think. It’s on a free hosting service, so I apologize in advance if it’s a little slow to respond.


One Week In


We have come a long way in a week. I started out not remembering the exact syntax for how to run a Ruby program. I felt a little silly, but we made it. Here is a look at some of the things we learned.

We started out small. We wrote a small program to keep track of pets for a humane society. Getting pet info from the user and storing it in an array so we could search and update stored values. It was a good first program to recall the skills we had learned during our prework.

We then moved on to a classroom management app. It read in a text roster, and helped create random groups for projects and homework. We had to learn how to read and parse a file, along with how to iterate through collections of data. It was also an opportunity to build on our understanding of Object Oriented Programming.

Then we hit a wall. We were tasked with scoring a game of bowling. That is a harder problem then it sounds. I had a really hard time figuring out what classes we needed and how to score spares and strikes. It was also the first program we tried to write using RSPEC and test driven development. This was by far the hardest program we had during the week, but we learned a lot.

Then it clicked. We wrote a Stock Portfolio application. We continued our Test Driven Development, and this time it just made sense. We were able to cruise through the assignment so quickly our instructor had to come up with more features for us to work on. It was an awesome feeling.

On Friday, we needed to play a game of Tic-Tac-Toe. It wasn’t so bad at first, until we got to artificial intelligence. Figuring out how to block a player from winning is harder than it sounds. We even had an opportunity for extra credit to modify our program to handle a board of any size. This was especially difficult, but we made it work.

Overall, I’m proud of the work we completed during the first week. I’m also looking forward to getting into web development with Sinatra next week.