Monthly Archives: November 2012

The cancellation of BritRuby and Why I love the Ruby community

While trolling through the internet, I’ve noticed some people stating that gender and race are real issues in the Ruby community and that the recent cancellation of the British Ruby Conference reflects poorly on the entire Ruby community as a whole. As a young, male, Hispanic American, I can tell you that this is complete B.S. As someone new to the Ruby community, and programming for that matter, you could say I have a pretty steep learning curve to overcome. And though I work hard everyday to learn Ruby, I wouldn’t have gotten as far as I am if it weren’t for the great people in this community. I’ve never met such smart and passionate people who genuinely love what they do and truly enjoy teaching others what they know. In the last few months, I’ve made friends with people in the Ruby community who regularly push me to get better and learn new things, folks who won’t hesitate to help me understand a new concept I may be struggling with. The point I’m trying to make is that as a non-white male new to the Ruby community, I have never had any problems with racism or with narrow-minded individuals such as those who state that the Ruby community has problems with race. The Ruby community is a supportive one, filled with great people who love learning and teaching others what they know. This is the Ruby community that I know and love.

Advertisements
Tagged , , , ,

What I learned today: Not All Methods Are Created Equal

Today I wrote a script in Ruby that, among other things, receives a text file as input and calculates the word count. One of the things I love about Ruby is that there’s always more than one method for doing things. But what I learned today is, not all methods are created equal. Let’s say we wanted to count the words in the following text:

What an excellent example of the power of dress, young Oliver
Twist was! Wrapped in the blanket which had hitherto formed his
only covering, he might have been the child of a nobleman or a
beggar; it would have been hard for the haughtiest stranger to
have assigned him his proper station in society. But now that he
was enveloped in the old calico robes which had grown yellow in
the same service, he was badged and ticketed, and fell into his
place at once–a parish child–the orphan of a workhouse–the
humble, half-starved drudge–to be cuffed and buffeted through
the world–despised by all, and pitied by none.

One way to accomplish this task is by using the scan method and passing the regular expression /\w+/ as an argument. scan iterates over a string and looks for a certain pattern passed to it as an argument, then outputs any matches into an array. So let’s say we store the Oliver Twist text above in a variable named text, and use the scan method to search for any word character using regular expressions, then ask to return the number of words found. Here’s how that would look:

Ruby scan method

Here, the scan method searched for all alphanumeric characters then returned the results into an array. The length method returns the number of words found. In this case, 113 words.

Another method we can use to count how many words are stored in the text variable is to use the split method. When no arguments are passed to the split method, it will automatically split the string by whitespace and return the results in an array. Passing the length method to that result will also return the number of words stored in the text variable. This is what that would look like:

Ruby split method

The split method returned only 107 words. Do you know why this may be? The reason is that by passing the regular expression, /\w+/, as an argument, the scan method counted the hyphenated words as two words, when they should have only been counted as one. So it seems to me that using split can provide a more accurate method to determine word count.

What do you think? Do you agree that using the split method can provide a more accurate word count, or can you use regular expressions to achieve the same result? Leave a comment below, I’d love to hear your thoughts.

Tagged , , , ,

Tutorial: Event Manager – by JumpstartLab

Today I built a program in Ruby called Event Manager that takes a CSV file containing ACTUAL attendee data from a political conference and manipulates that data based on certain instructions. If you’re new to Ruby or programming for that matter, I highly recommend it. Among other things, you’ll learn how to work with the CSV library and how to access a web-based API. The data file contains attendee names, addresses, phone numbers, emails, and the date and time they registered for the event. Problem #1 to solve was removing a lot of junk (incorrect street addresses and phone numbers). Here I learned a lot about string manipulation techniques. Then I learned how to output the cleaned data to a new CSV file. The fun part started when accessing the Sunlight API from the Sunlight Foundation, here I learned how to create a method called rep_lookup that looks up legislator data for each attendee based on their zipcode. I also learned how to create a method that pulls an attendee’s name and address from the original CSV file and creates a custom form letter in HTML. Awesome! Lastly, I learned how to do some analysis. I created methods that determined which days of the week and hours of the day most people registered for the event. This can be useful when determining the best times to run ads for a future event. All in all, this is a great tutorial for those wanting to learn Ruby. Check out the source code on my Github and the tutorial here.

Tagged , , , , ,
%d bloggers like this: