I'd use a recursive method with callbacks.
Since you're trying to evaluate a String
, you'll be best extending the String
class:
#config/initializers/string.rb #-> should be in /lib
class String
def has? *letters
letters.each do |letter|
yield letter, self.include?(letter)
end
end
end
#app
this = "allthisstuff"
this.has?("a", "f", "s", "d") { |letter,result| puts "#{letter} #{result}" }
# -> a true
# -> f true
# -> s true
# -> d false
The above will allow you to use a single block, through which you'll be able to evaluate the passed letter
:
this.has?("a", "f", "s") do |letter,result|
if result
case letter
when "a"
# do something
when "f"
# do something
end
end
end
--
If you wanted to include separate blocks (completely feasible with JS), you'd want to look at "callbacks". Although callbacks are not strictly part of the Ruby
way, you may be able to do it:
#config/initializers/string.rb
class String
def has? **letters
letters.each do |letter,lambda|
lambda.call(letter.to_s, self.include?(letter.to_s))
end
end
end
#app
this.has?({
a: Proc.new {|letter,result| # do something },
b: Proc.new {|letter,result| # do something else }
})
To improve this, it would be best to find the equivalent of arglist
in SASS
--
Refs: