ruby

recent posts related to the ruby programming language

Ruby CSV - An example of how to split CSV row data into fields

Ruby CSV FAQ: Can you share some sample Ruby code to demonstrate how to read a CSV file in Ruby?

I just created a Ruby script that would open, read, and parse a simple CSV file. If you're interested in the full class definition, the full Ruby source code is available here.

But if you're just interested in seeing how to parse the records and fields of a CSV file, keep reading here.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

The Ruby ternary operator syntax

Here's a quick example of some Ruby source code, showing how I used Ruby's ternary operator in a method that prints a CSV record for a class I defined:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Ruby - How to sort an array of objects by one field

Sorting an array of objects by one column in the object (class) is pretty simple with Ruby. Here's a quick demo of how I just did this when working on sorting the rows in a CSV file in a simple Ruby script.

Define the class

Step 1 in this is defining my class. So, here's the definition of my Ruby Person class:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to use Struct to simplify your Ruby class definitions

Many times when I'm creating a prototype of an application with Ruby I'll create my classes using a simple Struct technique I learned from a friend. This technique of using the Struct class makes my code shorter, and is at least as readable as writing a class out using any other method.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to open and sort a CSV file with Ruby

Here's the complete source code for a simple Ruby script which performs the following tasks:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

A Ruby substring example

Ruby substring FAQ: I can't find a Ruby substring method, how can I tell if one Ruby string contains another string?

Solution: I expected the Ruby String class to include a method named substring, but it doesn't. My personal disappointment aside, it does have a method named include? that works the way I expected it to. Here's a simple example, using the irbenvironment to perform a few tests.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to loop through each character in a Ruby String

Ruby character/string FAQ: How can I loop through each character in a Ruby String, and perform some operation on each character?

I'm currently using Ruby 1.8.6, and you can use the Ruby each_char method if you'll first require the jcode module. To be clear, this code will not work in Ruby 1.8.6:

a = 'hello, world'
a.each_char { |c|
  puts c
}

In fact it results in the following error:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

How to loop through each byte in a Ruby String

Problem: You need to loop through each character in a Ruby String, and get the byte value of the character as you iterate through the characters in the String.

Solution: Just use the each_byte method of the String class to loop through each byte in your String, like this:

a = 'hello, world'
a.each_byte { |c|
  puts c
}

This results in the following output:

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Ruby NameError: uninitialized constant error message

Problem: When working with some Ruby code in an irb session, I just got the following error message:

NameError: uninitialized constant Tempfile

Here's a snippet of my irb session where this error occurred:

>> tmp = Tempfile.new
NameError: uninitialized constant Tempfile
	from (irb):1

Solution

This "NameError: uninitialized constant" looks intimidating, but it's actually not a big deal: I just forgot to require the tempfile package.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

A Ruby temporary file example

Problem: There are times when you're writing Ruby code that you need to be able to create and write information to a temporary file. If you write your own code to do this, there are a number of decisions to be made (see below), and you'd like to avoid all of that mess.

Solution: Use the Ruby Tempfile class to handle the grunt work for you.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.