Blog

Exercise 12: Libraries: Learn Ruby The Hard Way: Practicum (TESTHEAD)

On November 9, 2011, in Syndicated, by Association for Software Testing
0

Anyone who has spent any time around Ruby has heard about gems and other files that can be called into your ruby scripts. There’s a variety of names for this, and the one that’s most common and understood by most developers and even casual programmers are “libraries”. A library is a collection or readily written items that we can call on and “the wiring under the board” has already been done for us.

Below is some example code that is being pulled in from a library. Note also that it uses some actual block structure (see the do and the end and the indentation of code. this is the first code we’ve seen formatted like this, which leads me to believe we’ll be seeing more stuff like it shortly).

So let’s take a look at this:

require ‘open-uri’

open(“http://www.ruby-lang.org/en”) do |f|
  f.each_line {|line| p line}
  puts f.base_uri         #
  puts f.content_type     # “text/html”
  puts f.charset          # “iso-8859-1”
  puts f.content_encoding # []
  puts f.last_modified    # Thu Dec 05 02:45:02 UTC 2002
end

[Here’s what I have written]

[and here’s what it does… and what it does is looks to take every line of ruby-lang.org and print it out in the format described]

So the first line uses a command “require”. What is it? What makes it special? And why should I be requiring it in the first place? The simple answer is that, instead of reinventing the wheel lots of code has already been written for us to call, to use, and to reference when we write our own code. Zed calls these “features” and that’s fine, but most anyone who’s had any involvement with a programming language recognizes a library when they see one :).

Extra Credit

Research the difference between require and include. How are they different?

[Hmmm… now this is interesting. From what I’ve read, it would seem like the include I used to use with C and require are very similar, in that for that given ruby file, the require command is sysnonymous with how other languages use “include”, meaning bring thee features or functions into this file and reference them as though they were in the file directly. Ruby’s include seems to be like other languanges “includes” only much more so, like at the direct language level. I’m not sure I get what that means, but it’s enough to know that require will be what I’ll more than likely use in projects and at the file level.]

Can you require a script that doesn’t contain a library specifically?

[To tell the truth, I don’t know at this stage. I would guess that, like any other language, we’d need to have some kind of function or module defined to need it to be included/required. Based on my experience at this stage, I honestly don’t know, but I’d say “probably not”.]

Figure out which directories on your system Ruby will look in to find the libraries you require.

[On my PC, it’s in C:\Ruby192\lib\ruby\1.9.1\ ]

TESTHEAD’s TAKEAWAYS

OK, that’s kind of interesting, and a bit unexpected. I’ve seen the require, and I figured it worked like include. I’m not sure I get why the different nomenclature; it seems to me if include is such a de-facto standard in languages, why change it in Ruby? Note, I don’t have the answer for that, it’s a question I have right now.

 

Comments are closed.


Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!