Re: The Ruby/ERB way to do this?

Thanks for taking the time to reply. Using a variable of require is a
good
idea.

Referencing variables as X::Size seems verbose. Is there any way for
require
to define a set of local scoped variables so the ERB template could drop
the
X:: prefix?

Well a couple of ideas there.

If you include the Module (X) then you don’t have to use the X:: prefix
when
referencing the constants they become part of the local namespace and
this
is true in the erb template which is executing using the binding.

So if you have a file myconstants.rb
module MyConstants
Foo = ‘bar’
end

And your program does this

require ‘erb’
require ‘myconstants’
include MyConstants

erb = ERB.new ‘my template here <%= Foo %>’

b = binding

puts erb.result(b) # gives you 'my template here bar

Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs