Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, how can I reference a lambda defined in a module?

This code used to work until I put my lambda function in a module.

Here is the lambda function:

module GalleryGenerator
  @add_links_to_descriptions = lambda do |mutable_viewable_content|
    mutable_viewable_content.description = add_links_to_sources(mutable_viewable_content.description)
    return mutable_viewable_content
  end
  #...
end

Here is how it is being used:

include GalleryGenerator

gallery = ViewableGallery.new(gallery_config.title, gallery_config.description, gallery_config.slug, \
gallery_config.sources, gallery_config.upload_date, gallery_config.map_url, gallery_config.map_title, \
gallery_config.year, viewable_photos).
update_using( \
    add_tabs_before_every_description_line(2), \
    @add_links_to_descriptions)

That's the error:

/home/mike/Development/Projects/FT/gallery_generator/lib/gallery_generator/viewable_gallery.rb:26:in `block in update_using': undefined method `call' for nil:NilClass (NoMethodError)
from /home/mike/Development/Projects/FT/gallery_generator/lib/gallery_generator/viewable_gallery.rb:25:in `each'
from /home/mike/Development/Projects/FT/gallery_generator/lib/gallery_generator/viewable_gallery.rb:25:in `update_using'
from bin/gallery_generator:32:in `<main>'

If the lambda message is not in a module, it all works. I suspect that it's looking for @add_links_to_descriptions in the wrong place now that's in the module...

How can I fix this? Thanks!

like image 848
Mike Avatar asked Feb 12 '23 04:02

Mike


1 Answers

try this:

module GalleryGenerator
  def self.add_links_to_descriptions
    lambda do |mutable_viewable_content|
      mutable_viewable_content.description = add_links_to_sources(mutable_viewable_content.description)
      return mutable_viewable_content
    end
  end
  #...
end

and call it with GalleryGenerator.add_links_to_descriptions

like image 199
dax Avatar answered Feb 13 '23 23:02

dax