I am mainly looking for a more beautiful way to write:
do_something() if my_string == 'first' | my_string == 'second' | my_string == 'third'
Any ideas?
The most common solution is to use Array#include?:
Array#include?
do_something if %w(first second third).include? my_string
do_something() if ['first', 'second', 'third'].include? my_string
do_something if my_string =~ /^(first|second|third)$/
/ , , , , :
require 'set' SPECIAL_VALUES = Set["first", "second", "third"] def foo(my_string) do_something if SPECIAL_VALUES.include?(my_string) end
"", "" .., Hash Set.
Hash
Set
Source: https://habr.com/ru/post/1753591/More articles:Написание полезных единиц измерения - unit-testingPorting C / C ++ to Android - androidHow to install TimeZone for my ASP.NET (MVC) application - timezoneFunction return is not assigned to a variable - functionWhat else do I need to know about implementing a system with a single password? - javaCcnet webdashboard authentication mode, how to configure it to be safe - ccnet-configDoes VBscript SendKeys support or support Unicode? - unicodeIs there a more pythonic way of changing No to `[]` than - pythonDRY programming with jquery - javascriptRobot.delay (int) vs Thread.sleep (long) - javaAll Articles