Skip to content

Instantly share code, notes, and snippets.

@xiangaoole
Last active January 26, 2021 14:17
Show Gist options
  • Save xiangaoole/599110f8913cd4ad00745b0d5ad412a4 to your computer and use it in GitHub Desktop.
Save xiangaoole/599110f8913cd4ad00745b0d5ad412a4 to your computer and use it in GitHub Desktop.
hyphen name to camel name
require 'set'
R = /
\b # word boundary
(?<part> # set group name as "part"
[a-zA-Z] # first character must be letter
[^ _-]* # the afterward character
)
(
[-_] # the hyphen or underline
\g<part> # get group pattern "part"
)+ # at least one
\b
/x # ignore comments
def find_hyphen_name(str)
result = SortedSet.new
str.split.each do |word|
if (word.match?(R))
result << word
end
end
return result
end
set = SortedSet.new
File.foreach(ARGV[0]) do |line|
set.merge find_hyphen_name(line)
end
puts set.to_a
R = /(?:(?<=^ )|[-_])[a-zA-Z][^ -_]*/
def to_camel_name(str)
str.gsub(R) do |s|
c1 = s[0]
case c1
when /a-zA-Z/ # first part
c1 + s[1..-1].downcase
else # afterward part
s[1].upcase + s[2..-1].downcase
end
end
end
puts to_camel_name("Little Miss-muffet sat_on_HE$R Tuffett eating-her_cURDS And_whey")
# => Little MissMuffet satOnHE$R Tuffett eatingHerCURDS AndWhey
# reference: https://stackoverflow.com/a/63771972/8673576
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment