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?

Where Next?

Popular Backend topics Top

New
Kurisu
Following on an old discussion I started on Elixir Forum here, I finally made my mind to learn Ruby on Rails in addition to Elixir/Phoen...
New
jaimeiniesta
I maintain a project that lists hundreds of thousands of web pages, and I’d like to show a screenshot for each web page. There are alread...
New
Fl4m3Ph03n1x
Background I am trying to up my Functional Programming (FP) skills and one of the things that newcomers first learn in FP is the Option T...
New
MarkIden
Hi, Recommend pls your favorite learning resources in Go, with best books, podcasts etc.
/go
New
Ora2pgnewbie
Wanted to check if there is a UI available to use Ora2pg tool.
New
sona11
I studied very basic PHP (I believe). After that, I feel like I’ve gotten a handle on the language. My dream is to work as a web develope...
New
Sumityadav
Hello all, I am new to learning Java Programming and want to learn java from scratch. I was writing a Java program to get the first and l...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
ogoldberg
Any recommendations on good resources for learning Elixir, Phoenix, and Ash?
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New