DEV Community

Discussion on: How Do You Pass a Method to gsub in Ruby?

Collapse
 
jonoyeong profile image
Jonathan Yeong • Edited

Hey Tiffany! If I understand your question correctly, you're trying to call page_partials and substitute the text {{ head }} with the value of page_partials.

If that's the case. You should be able to call that method directly in the gsub. Like so:

# Where new_page_partial is the file name of whatever page partial you want to pass in.
base_html.gsub("{{ head }}", page_partials(new_page_partial))
Enter fullscreen mode Exit fullscreen mode

It does look like your page_partials function might have a small bug. Here are my comments for it:

def page_partials(new_page_partial)
  # bug: page_partial isn't being referenced anywhere. You may want to use new_page_partial instead.
  pages = "site/#{page_partial}.html"
  new_page_partial = File.open(pages).read
end
Enter fullscreen mode Exit fullscreen mode

Hopefully this helps! I'm happy to pair on any Ruby code!

Collapse
 
tiffany profile image
tiff

Hey! Yeah we should definitely link up as I'm struggling with it a bit. It's pretty late on the East Coast, but I'll DM you on Twitter tomorrow if that's okay?

Collapse
 
jonoyeong profile image
Jonathan Yeong

Yeah for sure!