An ERB/bundler behaviour I don't understand

I have this code in a file that’s used to … render templates.

require 'erb'
require 'ostruct'

MISSING_CONFIG_MARKER = :config_key_and_value_missing

def render(template, configuration_data)
  config_object = OpenStruct.new(configuration_data)
  config_object.table.default = MISSING_CONFIG_MARKER
  ERB.new(template).result(config_object.instance_eval { binding })
end

I also have some tests for this, that show a behaviour I don’t understand:
When running the tests in the context of Bundler, one these fails (one that expects to find the MISSING_CONFIG_MARKER. However, when not running in bundler context, all tests pass.

I’d like to understand why that is, and ideally learn what I need to do to have the set default being used in the context of Bundler as well.

To illustrate all this I’ve set up a repository with a minimal (I think) project, showing the behaviour. See https://github.com/s2k/erb-templating

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

2 Likes

Did you find an answer to this after Stephan?

1 Like

Yes, in a way.

Over at the Bundler slack, someone mentioned that the test in question fails with and without using Bundler. When I experimented a bit more with other Ruby versions … results where even more confusing to me.
By now, I assume that something, messed up my Ruby installation (a Gem? env. variable? Ruby related dot file?) — After entirely removing RVM, reinstalling it and building a new Ruby 3.0.2 from scratch, I also got the test failure in both situations.

Meanwhile, I thought about the problem I was trying to solve and came to the conclusion, that it may be better to fail badly when trying to replace an ERB template because the value is missing, rather than inserting some default value and be surprised about it later.

Also I reduce the code above to this:

require 'erb'

def render(template, configuration_data)
  ERB.new(template).result_with_hash(configuration_data)
end

That’s much easier to understand, compared to setting the default for a hash and then using instance_eval and binding.
Using result_with_hash is much simpler (and something I didn’t know about before).

3 Likes

Glad you got it sorted Stephan :023:

Have you tried ASDF? I stopped using RVM years ago as it seemed most people were moving away from it. ASDF can be used for lots of languages too (I use it for Elixir, Erlang and NodeJS as well as Ruby).

There is a guide on my blog if it’s any interest to you: How to set-up a Ruby and Elixir dev environment on macOS (2021) – (via @AstonJ)

I use chruby in production as I find it is simple and easy.

1 Like

Another +1 for asdf!

2 Likes