s2k

s2k

Author of Fast Feedback Using Ruby

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

Most Liked

s2k

s2k

Author of Fast Feedback Using Ruby

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).

OvermindDL1

OvermindDL1

Another +1 for asdf!

AstonJ

AstonJ

Did you find an answer to this after Stephan?

Popular Backend topics Top

PragmaticBookshelf
Go is a modern programming language that combines the reliability of compiled languages with the ease of use and flexibility of dynamic t...
New
PragmaticBookshelf
Elixir and OTP provide exceptional tools to build rock-solid back-end applications that scale. Build a web application in a radically dif...
New
New
New
First poster: bot
Ruby on Rails v6.1.0 has been released. Link: Release 6.1.0 · rails/rails · GitHub
New
New
mafinar
Hello folks! We had a pretty fun thread here around the same time last year - talking about Advent of Code problems. That also happened t...
New
JimmyCarterSon
Hello, I am working on a new application with Elixir, Dish_out. I want to see Data I follow this tutorial with Elixir Casts. However, I ...
New
PragmaticBookshelf
Create Android applications using Jetpack Compose 1.6, Android Studio, Material Design 3, and the Kotlin programming language. Neil...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1016 16843 372
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New