Formatting Names Using Ruby

Getting information from users is a common task that we have to deal with in building an application. This typically happens either in creating a user account, commerce orders, or any other time when the application has to ask “who are you?”. Often times we’ll create a field for both a firstname and a lastname allowing the user to separate their names to make it easier for us (the application developer) to display either/or both. In this post, I’m going to talk about how we can format full names given to the application by the user without necessarily worrying about creating two textfields. Of course, you would use this code in the methods within a Rails app.

So let’s say I have a variable for creative purposes, I’ll call it ‘name’ and ‘name’ has a value.

name = “James Earl Jones”

And I want to only display the first and last name of my ‘name’ value I could do this:

names = name.split
firstname = names.first
lastname = names.last

Now this splits the three words in ‘name’ into an array. I then set firstname as the first item from the array, and set lastname as the last item from the array.

p “#{firstname} #{lastname}”

Would then output:
“James Jones”

How about we do another common formatting task we can to show the last name first separated with a comma the rest of the name, we could do this:

names = name.split
lastname = names.pop
firstname_remaining = names.join(‘ ‘)

Here we split the name up again, put then ran the pop method which pulls the last time of the array out of the array and then returns it. This would leave only the remaining name left to work with as the firstname_remaining variable. I them join the rest of the array with spaces for the firstname_remaining (which would include the first name and any remain name information like a middle name).

p “#{lastname}, #{firstname_remaining}”

Would then output:
“Jones, James Earl”

Lastly, a common thing I’ve been seeing lately is the usage of the last initial when showing a user’s name. We could do something like this:

names = name.split
firstname = names.first
lastinitial = names.last[0,1]

Here I’m introducing some trimming for the lastinitial variable. You see this with a bracket and two numbers. The first number is the offset in the string that I’m trimming (zero is the first letter is a string), the next number is how many characters I’m showing from that offset.

p “#{firstname} #{lastinitial}.”

Would then output:
“James J.”

So that’s it for now. Of course you might think about adapting this to users that will have items at the end of their name like “Jr.” – this can sometimes just be solved using a drop-down.  I’m hoping that perhaps I have introduced you to some things in Ruby that you might have been previously unaware of. Until next time.


Silencing Rails Logs for Security

For those of you that are already familiar with the Rails framework you already know that the Rails logs are completely invaluable when it comes to debugging and troubleshooting issues.  However, just as with any form of transaction logs they introduce a major security issue.

Just think about some of that information that you might be storing in your database… credit card numbers?, social security numbers?, or what about passwords?  Imagine someone getting a hold of those logs and seeing all the session parameters being passed into the database.  Inserting the credit card numbers into an order, selecting the user information based on a username and password combination.  See the point I’m making here?  Let’s look at a quick example of what you’ll see in your logs:

Parameters: {“model”=>{“username”=>”John”, “password”=>”hax0r”}…

Not good.

Thankfully Rails makes it simple to filter this important material from your logs.  Just open up the model that you want to filter and add a single line just below the class definition:

class Model < ActiveRecord::Base
filter_parameter_logging :password


In this case I just filtered “password”, but you could filter whatever else you wanted to for that model, separating each parameter with a comma, like such:

filter_parameter_logging :password, :confirm_password, :ssn, :creditcard_number, :etc

It’s that’s simple.  So keep those Rails logs secure and filter out the stuff that others don’t need to know.

Using Ruby to Stay Informed With Innovative Thought

So I was thinking this evening about creating an RSS parser in Ruby. You know… Ruby supports this built in? Big surprise right? All you have to do in require the rss library:

require 'rss'

Then of course if you want to open up a connection to a URL you need to include the open uri library too:

require 'open-uri'

So that’s all the requirements. Next I’ll create a method called ReadRss that will take a single variable defined as “url”.

def ReadRss(url)
  open(url) do |page|
    respond =
    result = RSS::Parser.parse(respond,false)
    puts "Blog: #{}, #{}"
    result.items.each_with_index do |item, i|
      i += 1
      puts "#{i}  #{item.title}"

That’s it. Now all you have to do is call ReadRss with the site feed address. Here’s a good hint for you:


So now that you can parse RSS feeds right from your Ruby script.  ATOM parser will come shortly.

Web 2.0: Day 2 Recap – Sessions

Of course I am recapping here and reviewing most of my notes now that I am home. I still wanted to share what I had experiences so I’m posting this information a little late. Day 3 and 4 will be coming as well.

The New Hybrid Designer

This was a panel discussion that included Kelly Goto, Jeremy Keith, and Chris Messina. Unfortunately it become more of an introduction to the Design related track that really getting down the what it means to be a Hybrid Designer. Getting the designers to learn more about application design and architecture are some of the most important key points here. Using documentation such as that from Apple, their Application Design Guidelines is a great suggestion. Remembering as well that the line between design and development continues to grow thinner. Continuing to place strong consideration on “placelessness” – the idea that not only should content be separated from design but as well as context and device limitations. Chris Messina also made strong mention against applications such as Adobe’s Apollo which will end the “View Source” option, noting that many of todays developers have learned using the method of learning from someone else’s work. I was differently that person and I’m sure many of today’s beginners learned HTML are doing the same. It is important we don’t kill the growth of our community by developing applications that eradicate it’s growth.

Rich Internet Applications with Apollo

Sadly, the presentation with Mike Chambers as he tried to show the benefits of Apollo left me desiring more in general. I can’t blame Mike for it completely because the network was extremely congested and he was unable to demo many of the features of online application access. The thing that really has got me bothered by the platform in general is that, in a bad way, it feels like “half a product”. Now I’m a strong advocate of building “half a product” more then a “half ass product”. Perhaps I would lean to being more enthusiastic about this product if I felt the features planned for inclusion in their initial release was the “correct half” of the product.

If you are wanting to streamline application development to “bridge the gap” between the web and desktop platforms you need to create a way to easy deploy the single page/controller level updates to all the desktop clients. Streamlined, without interruption – with no option to not update the functionality. It would be a replica of the features you are mimicking from the web application you are converting. Not necessarily in user interface, but function and user experience.

Vulnerabilities 2.0 in Web 2.0: Next Generation Web Apps from a Hacker’s Perspective

This was an amazing conference session. Given by a partner of iSEC Partners a security research firm and pen-testing company. I’m hoping to get a copy of the slides as the presenter did tell us that they would be available. Getting into topics that were far more advanced then just simple cross-site scripting issues. Major vulnerabilities exist in all current AJAX framework implementations as well a big issue with most AJAX sites is that the functions and methods are rightly available to all visitors to the application. Having methods within your code for “MakeMeAdmin()” is ridiculous! But it still happens. Remembering as well using cross site forgery techniques are assisted because browsers will pass the cookie if it is active in the other window or tab – because cookies are shared among windows. It turns out the guys over at iSEC Partners are going to be publishing the new Hacking Exposed book in December 2007 entitled ‘Hacking Exposed: Web 2.0’.

The Arrival of Web 2.0: The State of the Union on Browser Technology

I’ll be honest and say I don’t know how much really came out of this session other then, “Browser companies are starting to work today.” People representing Opera, Mozilla, and IE were on the panel. Other then continuing to hear that Firefox 3 will offer local store so you can natively develop offline applications and that the Mozilla foundation is working on issues that exist in JavaScript as it is currently being implemented using Ajax (the previous session was of course stuck in my head at the time). That was about it on that one.

Cleaning up my Ruby Fizzbuzz

As I become more familiar with Ruby and Rails I’m of course going to start to understand better ways to do a snippet of code. Here is an updated script that is a little leaner:

(1..100).each do |i|
  fb = []
  fb << "Fizz" if (i % 3) == 0
  fb << "Buzz" if (i % 5) == 0
  fb << i if (i % 3) != 0 and (i % 5) != 0
  puts (fb.join "")

I am still tring to review my notes, so I just ask that those of you awaiting my review of the Web 2.0 Expo please continue to be patient.

Web 2.0: Day 1 – Workshops

So today was the start of the Web 2.0 Expo for those of us attendees registered for the workshops. The official beginning tomorrow for the other sessions and expo hall.

I’m slowly getting used to Pacific Time, thought it’s 10pm here and I think I’m still about 1-2 hours off. So the workshops…

The Iterative App: From Discord to Design

The day started with Kelly Goto – a favorite author of mine who wrote “Web ReDesign” which was all about merging User Centered Design with Agile Development Methodology. It was the biggest high I had received in a long time when I was sitting there in the audience and here this author that I respect so greatly was pushing the same techniques and still that I’ve been trying to evangelize. The idea that you really need to target your users emotional usage of your product more then anything for it to me successful. That you just need to “pick a feature” – and start with just that feature it in the beginning building off of that along with what you users actually are doing with your product. Her example was Flickr talking how it originally began as a gaming platform, but everyone was simply using the photo sharing portion – so it evolved with how people were using it. Products most evolve of time! Small pieces growing toward a closer idea of what the user NEEDS (not wants). She uses a great quote from Henry Ford of, “If I would have asked people what they wanted, they would have told me a faster horse.” Products must solve a “need” and not be created from a “want”. Plus she filled in a lot of hole in my of my methodology theory that involved research and testing.

The second half of her workshop was a little touchy as many of her slides where out of place and we were running late. Thankfully she promised all the finished slides would be available at a special URL for our usage and study. She even hinted at another book grouping UCD ideals with Agile rather then the previous “Core” framework she previously developed. I’m highly looking forward to seeing that come to market.

All in all, but myself and the other architect with me that attended Goto’s workshop released at every level that our process just wasn’t cutting it in the real world.

Ruby on Rails with David A. Black

I wasn’t sure what I should expect from David’s workshop so I walked in pretty open minded knowing I would now have an outlet to have many of my questions answered. The room was filled with Java programmers looking to prove Java better as a language. I felt bad for them. Here they are in their J2EE world and this little language has been growing and taking many Java jobs away. The wanted to stand up for their language. So many questions came regarding performance and if Ruby could actually be faster the Java or even compare to it. David was very patient and answered all of their questions as best as we could without he, himself being a Java Developer.

After the presentation we took a break were David fielded some questions prior to him taking a break himself. As I was answering one gentleman’s question regarding development with Ruby and Rails I found myself with a growing audience from all areas of the world. Here I was, little ol’ me talking to PHP programmers, Java Programmers, .NET Programmers, C Programmers, and even a single ColdFusion developer. I only hope that I articulated the benefit of Rails well enough, but it felt good explaining my point of view on Rails and to get feedback from other developers.

Once we regained the group together it was time to write some Rails code. I spent some time with David asking my small questions that I just couldn’t get my head around which he handled willingly and I feel far better equipped to deal with certain application types now. But I think too he was happy to have someone there that could “get his back” because he didn’t feel right about defending one language over another. He also showed me some great things in TextMate that I can use when developing Ruby which I didn’t know before. It was definitely worth it to me to go because I got to see what I knew, and have questions answered that I just could figure out before.

So day 1 was great. I even got a free O’Reilly book that I’ve been wanting called “Designing Interfaces” – essentially a usability book. Tomorrow morning beginnings the sessions where I will be seeing Kelly Goto again regarding the “Hybrid Designer” along with Mike Chambers from Adobe regarding the new Apollo application. I’m going to try to get some of his feedback regarding the new features in Firefox 3 regarding local data storage along with Joyent’s Slingshot framework. I hope to really get a solid understanding of what the benefits of Apollo will be in comparison to what these other’s are doing.

Until tomorrow…

Preparing for CSS Naked Day with Ruby on Rails

So you are sitting there around the house watching reruns of Smallville and Seinfeld and you think, “I wish I could jump onto the CSS Naked Day bandwagon with my Ruby on Rails application”, well you are in luck. Conditional statements in Rails are a piece of cake and with logical expressions like “unless” we save ourselves a ton of code.

In our layout when we call our stylesheet link tag we would just add some conditional code to the end like this:

<%= stylesheet_link_tag "default"
unless ( == 4) and ( == 5) %>

And that’s it, show the stylesheet unless it’s April 5th. The carriage return is for the blog, this can go on one line. I do have to say, be nice to your users here, you just might break the shit out of some of your UI without your CSS. Most Rails applications have more then just a blog on them so I suggest not doing this on a production application (do I really need to be saying this?).