ruby

recent posts related to the ruby programming language

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.

How to append text to a file with Ruby

Ruby file FAQ: How do I append text to a file in Ruby?

Solution: Appending text to a file with Ruby is similar to other languages: you need to open the file in "append" mode, write your data, and then close the file.

Here's a quick example that demonstrates how to append "Hello, world" to a file named myfile.out in the current directory:

A Ruby write to file example

Ruby file FAQ: How do I write to a file in Ruby?

Writing to a file with Ruby

Many times when you're working with Ruby scripts you need to be able to write text information to a file. Writing text to a file with Ruby is reasonably straightforward. Just like many other languages, you need to open the file in "write" mode, write your data, and then close the file.

A Ruby current date and time example

Ruby current date/time FAQ: How can I determine the current date and time in a Ruby script?

You can create a Time object to represent the current date and time by calling the Time.now method, like this:

How to process every line in a text file with Ruby

There are quite a few ways to open a text file with Ruby and then process its contents, but this example probably shows the most concise way to do it:

# ruby sample code.
# process every line in a text file with ruby (version 1).
file='GettysburgAddress.txt'
File.readlines(file).each do |line|
  puts line
end

As you can see, this example code is very concise, and inside the processing loop you can do whatever you need to do with the line variable.

Ruby - Variable length argument lists with Ruby

Ruby FAQ: How do I create a variable length argument list in a Ruby method?

One thing I really dig about Ruby is that I can create methods and functions that support variable-length argument lists. It's not something you need all the time, but it sure is nice to have it when you need it.

Here's how you create and then call a Ruby function/method that can take a variable number of arguments:

How to show Ruby class documentation from the command line

For some reason I can never remember the command to show Ruby documentation (the rdoc stuff that is a lot like Javadoc) from the command line, so this blog post is especially for me ...

To show Ruby rdoc documentation from the command line, just use the ri command, like this:

Ruby 'execute shell command' examples

Ruby exec FAQ: How do I execute a shell command from a Ruby script?

It's very easy to execute an external shell command from a Ruby script. Off the top of my head there are at least two ways to do this. First, you can use the traditional backtick operators. Second, you can use the %x syntax. I'll demonstrate both examples here.

Use the backtick operator

First, I'll use the backtick operator to execute a shell command. Here's how you can run a simple command, like the ls command, using Ruby and the backtick operator:

How to open a file and read its contents using Ruby

Ruby file FAQ: How do I open and read from a file in Ruby?

How many ways are there to open a file with Ruby and process the file contents? I don't know for sure, but here are two different ways to do it.

Ruby file processing, version 1

First, I'll use Ruby and the File.open method to open a file and process its contents, like this:

Ruby directory list - How to use Ruby to list files in a directory

It's so easy with Ruby to get a list of files in the current directory that I hesitate to write this, but hey, this blog is for me and my bad memory, so here's a quick note on how to use Ruby to get a list of files of a certain type in a directory.

To have a little fun with this, I'll use irb (the interactive Ruby shell environment) to show how to do this.

Syndicate content