slayedbylucifer
slayedbylucifer

Reputation: 23502

how to use erb Template in ruby code

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.

Upvotes: 4

Views: 3712

Answers (2)

matt
matt

Reputation: 79733

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.

Upvotes: 4

user2845360
user2845360

Reputation:

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.

Upvotes: 0

Related Questions