4

I want to use the erb template feature of ruby. But I am not able to run the sample code provided.

I find that erb is installed:

# file /usr/lib/ruby/1.9.1/erb.rb
/usr/lib/ruby/1.9.1/erb.rb: ASCII English text

and it was installed along with ruby:

# dpkg -S /usr/lib/ruby/1.9.1/erb.rb
libruby1.9.1: /usr/lib/ruby/1.9.1/erb.rb

I am running below sample code which is provided in /usr/lib/ruby/1.9.1/erb.rb

#!/usr/bin/ruby
require 'erb'
x = 42
template = ERB.new <<-EOF
  The value of x is: <%= x %>
EOF
puts template.result(binding)

I get below error when I run the code:

/workspace/ruby/erb.rb:4:in `<top (required)>': uninitialized constant ERB (NameError)
    from /workspace/ruby/erb.rb:2:in `require'
    from /workspace/ruby/erb.rb:2:in `<main>'

Question: what is missing in above code?

Here is my platform:

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 10.04.4 LTS
Release:    10.04
Codename:   lucid

# uname -m
i686

and my ruby version:

# ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [i486-linux]

ANSWER:

@matt's comment is answer to this question. I had named my script as erb.rb and that was the problem. I renamed it to testing_erb.rb and it worked. Please check matt's comment for more details.

slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123
  • a fair guess is that the ruby you are running (`/usr/bin/ruby`) is not aware of the libraries in `/usr/lib/ruby/1.9.1` for some reason. a [question about the ruby load path](http://stackoverflow.com/questions/837123/adding-a-directory-to-load-path-ruby) may give you some insights into this. are you using rvm or rbenv mayhaps? if you are lucky just running `gem install erb`may solve your problem. – froderik Oct 04 '13 at 10:11
  • `/usr/bin/ruby/` is actually a symlink to `/usr/bin/ruby1.9.1`. and I am using only 1.9.1. No other ruby version is installed on my box. I also had tried `gem install erb` but it fails saying `ERROR: could not find gem erb locally or in a repository`. Thanks. – slayedbylucifer Oct 04 '13 at 10:30
  • forgot that erb is part of the standard library. so your ruby installation is not feeling well at all. I would try to reinstall ruby or something like that. not suer about how to do that on ubuntu. – froderik Oct 04 '13 at 11:10
  • 2
    Your test file is called `erb.rb`. When you `require 'erb'` it is trying to require _itself_, which is why you get your error (the current directory is on Ruby’s load path in 1.9.1). Try renaming your file to something like `erb_testing.rb`. It may be that this gives you a slightly different error – I can’t actually reproduce it since the standard library version of `erb` appears before `.` locally and so gets loaded first – there may be some other problem with your installation. – matt Oct 04 '13 at 12:15
  • @matt. That was it. I changed the name of my script to `testing_erb.rb` and it worked. Thank you so much. Please add it as an answer so that I can accept it. Thanks again!! – slayedbylucifer Oct 04 '13 at 13:02

2 Answers2

4

Your test file is called erb.rb. In Ruby 1.9.1 the current directory is on the load path, so when you call require 'erb' it is trying to require itself. Since the file does not define ERB you get the uninitialized constant error when trying to use that constant.

Try renaming your file to something like erb_testing.rb, so that when you require erb there is no confusion between the different files.

Note that in later versions of Ruby (1.9.2+) the current directory is not in the default load path, so you wouldn’t see problems like this. It might be worth looking at upgrading your version of Ruby since 1.9.1 is pretty old now (2.1 should be released soon, and with that 1.9.1 will be four versions behind). Take a look at RVM if your package manager doesn’t have more recent releases.

One thing to note is that this error depends on the order of the entries in the load path. On my install of 1.9.1 the current directory is the last entry, so when requiring erb the version from the standard library is seen first and loaded, and there is no error. It looks like your installation puts the current directory before the standard library – I don’t know why that would be though.

matt
  • 78,533
  • 8
  • 163
  • 197
0

Sounds like your ruby installation is somehow broken. If you really want to use system ruby, why not try removing ruby and then installing it again, e.g. apt-get -y remove ruby && apt-get -y install ruby.

Another option is to use Ruby Version Manager (RVM, see http://rvm.io/) and make use of it to get a clean ruby interpreter installation to your box. RVM is actually the recommended way of using ruby nowadays, as most distributions ship somewhat aged ruby interpreters in their packages.