Guest User

Untitled

a guest
Feb 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class AClassThatUsesSend
  2. def action
  3. # here's where the action happens
  4. @helper_type = "foo"
  5.  
  6. # this is equivalent to self.foo_helper or just foo_helper
  7. send("#{@helper_type}_helper")
  8. end
  9.  
  10. def foo_helper
  11. #more fun here
  12. end
  13. end
  14.  
  15. # of course you don't want to just take user input unfiltered and feed it
  16. # into send() for possible safety concerns, so consider something like this if
  17. # @helper_type comes from a param
  18.  
  19. valid_helpers = ['foo', 'bar', 'quux']
  20. send("#{@helper_type}_helper"} if valid_helpers.include? @helper_type
  21.  
  22. # There are many ways you could sanitize the value of @helper_type.
  23. # This is just one approach.
Add Comment
Please, Sign In to add comment