How To Convert An Array Into A Regular Expression

preview_player
Показать описание
In this video you will learn a nice little trick to convert an array of strings into a regular expression that you can use with the gsub method.

This allows you to map substrings into other strings the easy way!

You will find how to combine these concepts:

- Regular expressions
- Hash objects
- Strings

For more awesome Ruby tutorials subscribe to the channel now & visit my site:

Thanks for watching! :)
Рекомендации по теме
Комментарии
Автор

You always surprise me with Ruby features I never knew existed before. Thanks for the video.

MazinHakeem
Автор

Thanks for the instructive video. Following your encouragement, I played around a little, trying to find alternative Ruby ways leading to the same result:

#building the Regexp with a string, using Regexp.new
#(note however that Regexp.union can do much more than just simulate |)


#alternative to this: /.../ also accepts string interpolation!
food.scan(/#{a.join("|")}/)

#the other way round: Array intersection, without even using Regex.
#not that this will unify the entries - will report bacon only once
food.split(" ") & a

christianzellweger
visit shbcf.ru