
Zeff
Sustainable Web Development with Ruby on Rails:
Title: Sustainable Web Development with Ruby on Rails: Regex incorrect (testing Helpers section)
This code fails:
“# test/helpers/application_helper_test.rb
class ApplicationHelperTest < ActionView::TestCase
test “styled_widget_id” do
rendered_component = styled_widget_id(“1.23”)
→
→ regexp = %r{
→ <span # match a span tag
→ [^>]* # ignore anything that isn’t >
→ > # match the > to close the opening tag
→ .* # anything at all in here (e.g. other tags)
→ 1.23 # the widget ID we expect, escaping the dot
→ .* # anything after it (e.g. other tags)
→ # closing span tag
→ }x
assert_match regexp, rendered_component
assert rendered_component.html_safe?
end
end
The line:
→ [^>]* # ignore anything that isn’t >
should be:
→ .* # ignore anything that isn’t >
First Post!

davetron5000
I’m surprised this fails. The book executes this code when it’s run and that regexp should be correct.
Without the %r{..}x
formatting, the regular expression in the book is:
regexp = /<span[^>]*>.*1\.23.*<\/span>/
And this does match the two strings in the book:
> regexp.match?('<span style="font-family: monospace">1.23</span>')
=> true
> regexp.match?('<span class="mono">1.23</span>')
=> true
I did notice that copying and pasting from the PDF doesn’t copy the regular expression properly. Unfortunately, the tools I use to build the book result in weird uncopyable code blocks.
Can you try copying the code straight from the .zip
files and see what happens?
Popular Pragmatic Bookshelf topics








Modern front-end development for Rails, second edition - Struggling to get the first chapter to work


Other popular topics









Latest in Sustainable Web Development with Ruby on Rails
Latest in PragProg Customers
Latest (all)
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /html
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim