Sunday, January 10, 2016

Ruby Basic String Methods

Need a reference text for the Ruby programming language? Get the Well-Grounded Rubyist.

This article will briefly go over Strings in Ruby.

First, let us talk about a few String methods. You can see all the methods available to a string if you call the "methods" method:

"Some String".methods

=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, 
:insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, 
:succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, 
:chr, :getbyte, :setbyte, :byteslice, :scrub, :scrub!, :freeze, 
:to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, 
:capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, 
:hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, 
:reverse!, :concat, :<<, :prepend, :crypt, :intern, :to_sym, :ord, 
:include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, 
(output omitted)
:public_send, :respond_to?, :extend, :display, :method, :public_method, 
:singleton_method, :define_singleton_method, :object_id, :to_enum, 
:enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, 
:__id__]

There are many methods you could use, but here we will just talk about a few.

To start off, if you want to change the case of a string, you can either make it upper case or lower case with the following methods:

"Hello".upcase
"World".downcase

Output:

=> "HELLO"
=> "world"

There is a method that capitalizes the first character in the string and makes everything else lowercase:

"How ARe You dOIng toDAY?".capitalize

=> "How are you doing today?"

You can get rid of trailing spaces (either from the left or from the right) using the lstrip/rstrip method:

"   , And then ".lstrip

=> ", And then "

"   , And then ".rstrip

=> "   , And then"

Now, if you had those strings in a variable and you called all those methods, the original string would not change. That is, you would get a copy of the original string that was transformed according to the method you called. If you really want to change the original string, you would have to use the exclamation point, like this:

my_word = "Hello"
# Uppercases my_word permanently
my_word.upcase!
puts my_word

Output:

=> "HELLO"

Compare the use of no exclamation with one actually present:

sentence = "I climbed a tree."
another_sentence = sentence.upcase

puts "Original sentence: #{sentence}"
puts "Another sentence: #{another_sentence}"

sentence.upcase!
puts "Now sentence is: #{sentence}"

Output:

Original sentence: I climbed a tree.
Another sentence: I CLIMBED A TREE.
Now sentence is: I CLIMBED A TREE.

So in the code above we define a string variable and then create another string variable that takes the uppercase version of that original variable. Upon printing both of those variables, you see that the original is still unchanged while the second one has all its letters uppercase.

The code above uses string interpolation to display the value of a string variable. If you don't know about this, it is simple: within a double-quoted string, use #{} and place the variable name inside to actually display its value when you use puts. That is, puts "Something bla bla #{some_variable} will be displayed"

Now, moving on. The second part of the code calls the upcase method with a bang, !. Now, the actual variable will be modified. Whatever is stored in sentence will be turned into uppercase permanently. When you call puts to display the value of sentence, you see it is indeed a string with all characters uppercase.

Let us now talk about other more advanced methods. Before that, know that you can access any character in the string using standard array access notation:

my_string = "Somewhere over the rainbow"
# Displays the fourth character in the string (counts from 0)
puts my_string[3]

Output:

=> e

A useful method to search for a substring is the include? method.

"Welcome to my world".include? "world"
=> true

"Welcome to my world".include? "to"
=> true

"Welcome to my world".include? "earth"
=> false

So the method returns either true, if the sequence of characters was found in the string, or false, if the sequence of characters was not found.

The include? method can also take just a single character, if you use either "x", 'x', or ?x, where x is any character.

"Hello".include? 'e'
=> true

# Does it include the character e? (same as above)
"Hello".include? ?e
=> true
Now, another useful method is the sub method. You can substitute characters in a string with something else.

"I went to the gym today.".sub 'gym', 'theater'

Output (irb):

irb(main):023:0> "I went to the gym today.".sub 'gym', 'theater'
=> "I went to the theater today."

So the method sub substitutes a given string with a new string. Note that it does not change the original string, but makes a copy. If you want to actually change the original string (when working with variables), use the exclamation point:

sentence = "I ate pasta yesterday."
another_sentence = sentence.sub 'pasta', 'cake'

puts sentence
puts another_sentence

# Modify "sentence" variable permanently
sentence.sub! 'pasta', 'cake'
puts sentence

Output:

I ate pasta yesterday.
I ate cake yesterday.
I ate cake yesterday.

Note that the sub method substitutes only once. That is, the first match it finds will be the only one changed:

irb(main):002:0> sentence = "I love to eat a lot of pasta pasta much pasta"
=> "I love to eat a lot of pasta pasta much pasta"
irb(main):003:0> sentence
=> "I love to eat a lot of pasta pasta much pasta"
irb(main):004:0> sentence.sub 'pasta', 'pizza'
=> "I love to eat a lot of pizza pasta much pasta"
irb(main):005:0> sentence
=> "I love to eat a lot of pasta pasta much pasta"

That is all for this article. We talked about how you can modify strings using upcase, downcase, capitalize, as well as how to modify the original array using the exclamation (!) point. Then we looked at how to find matches in a string using include? and how to substitute matches with the sub method. There are many methods out there, so you could use the method "methods" to find out which methods are available to each class object.

Looking for a reference text for Ruby? Get the Well-Grounded Rubyist.

Sunday, January 3, 2016

Basic File I/O in Ruby

Need a reference text for the Ruby programming language? Get the Well-Grounded Rubyist.

Reading data from a file as well as writing data to a file has been made easy with Ruby. This article will cover how to read from a file and how to write to a file using Ruby's File class.

Let us jump right into it: to read each line from a file, use a block for the foreach() method of the File class.

File.foreach('somefile.txt') do |line|
  puts line
end

Each line will contain a \n newline character, so if you would like to chop it off, use

line.chomp

Assuming somefile.txt contains the following contents:

This is a sample file
With some contents within
This is the last line :)

The output of the Ruby script would be:

$ ruby basic_file_io.rb
This is a sample file
With some contents within
This is the last line :)

So you iterate through each line of the file and do something with that line. Specify the filename as the method parameter and then do a block with a parameter that will be used to hold each line, |line|.

There is one problem with the above construct: what if the file did not exist? Then surely you will get a problem:

basic_file_io.rb:1:in `foreach': No such file or directory - doesnt_exist.txt (Errno::ENOENT)

I tried to open a file called "doest_exist.txt" to read, but it did not exist, so I got an error.

To get around that error, you can either: handle the exception, or first check if the file exists before trying to do anything with it.

The following code illustrates how to handle exceptions:

begin
  File.foreach('doesnt_exist.txt') do |line|
    puts line
  end

rescue Exception => e
  puts "Oh no, something went terribly wrong!"
  puts e.message
  puts "Maybe we can do something about that problem here . . ."
end

Output:

$ ruby basic_file_io.rb
Oh no, something went terribly wrong!
No such file or directory - doesnt_exist.txt
Maybe we can do something about that problem here . . .

As you can see, you could have done something about the problem using the exception construct. You start off by saying "begin" then put everything you are attempting to do right afterward. Then, you make a "rescue" section with an object to hold the data for the exception; I used the name "e" above. Within the rescue section, you can determine how the program should behave in case some problem happens. Using e.message, you can also see the description of the problem. Finally, don't forget to finish everything with an "end."

Another way to handle problems when trying to read a file is using the "exist?" method. If you don't want to use exceptions, then you can try this out:

if File.exist? 'sample.txt'
  File.foreach('sample.txt') do |line|
    puts line.chomp
  end
else
  puts "I am sorry, but the file does not exist."
end

Assuming sample.txt does not exist, the output is:

I am sorry, but the file does not exist.

Now, let us see how to write to a file. In a similar fashion, we use the File class and call the open() method with a block that will do something to the file:

File.open('newfile.txt', 'w') do |file|
  file.puts "Hello everyone!"
  file.puts "This will be a brand new file!"
  file.puts "Just one more last line"
end

The above will open the file "newfile.txt" for "write." That is, a new file will be created and IF there is already a file named "newfile.txt," it will be completely wiped out and OVERWRITTEN. So be careful with the "w" mode. :) Moving on, you just use the |file| parameter to add things to the file. Just call file.puts with whatever string you want to add to the file contents. Because "puts" adds a newline character, each of those sentences written in the code above will be in their own line. When you take a look at the file, you get this:

$ cat newfile.txt
Hello everyone!
This will be a brand new file!
Just one more last line

Pretty cool, isn't it? Now, there are other modes that let you append to files, etc. I will leave it to you to Google it and find out about them if you don't already know. Here is a big hint: http://stackoverflow.com/questions/3682359/what-are-the-ruby-file-open-modes-and-options

And to conclude everything, just an important note: files are automatically *closed* at the end of each block, so there is no need to call a method to close the file, like in other programming languages such as C++ and PHP.

In summary, Ruby lets you handle file I/O with the File class. You can iterate over a file to read each line using the File.foreach() method. There might be cases in which the file will not exist, so you can handle issues with an exception (begin-rescue-end construct). There is also the option to use the File.exist? method to check whether a file exists. Writing to files can be done using the File.open() method in combination with a block with instructions to add something to the file. You can use the puts method on the block parameter to add some content to the file.

Looking for a reference text for Ruby? Get the Well-Grounded Rubyist.