Skip to content

Instantly share code, notes, and snippets.

@tallenaz
Created March 18, 2014 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tallenaz/9631840 to your computer and use it in GitHub Desktop.
Save tallenaz/9631840 to your computer and use it in GitHub Desktop.
Manipulating strings, Breaking Bad style...
require 'colorize'
periodic_table = %w[H He Li Be B C N O F Ne Na Mg Al Si P
S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu
Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo
Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe Cs
Ba Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb
Bi Po At Rn Fr Ra Lr Rf Db Sg Bh Hs Mt
Ds Rg Cn Uut Fl Uup Lv Uus Uuo La Ce Pr
Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Ac Th
Pa U Np Pu Am Cm Bk Cf Es Fm Md No]
name = ARGV[0]
name_downcased = name.downcase
matches = []
periodic_table.each do |element|
element_downcased = element.downcase
if name_downcased.include? element_downcased
matches << element
end
end
if matches.empty?
puts name
else
random_match = matches.sample
colorized_match = random_match.green
if name.include? random_match
puts name.sub(random_match, colorized_match)
else
puts name.sub(random_match.downcase, colorized_match)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment