forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeCase.rb
More file actions
38 lines (28 loc) · 831 Bytes
/
SnakeCase.rb
File metadata and controls
38 lines (28 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
=begin
Complete the function/method so that it takes CamelCase string and returns the
string in snake_case notation. Lowercase characters can be numbers. If method
gets number, it should return string.
Examples:
# returns test_controller
to_underscore('TestController')
# returns movies_and_books
to_underscore('MoviesAndBooks')
# returns app7_test
to_underscore('App7Test')
# returns "1"
to_underscore(1)
=end
# My Solution
def to_underscore(string)
result = ""
string.to_s.split("").each_with_index {|x,i| x == x.upcase && i != 0 && x =~ /[a-zA-Z]/ ? result += "_" + x.downcase : result += x.downcase}
result
end
# Better Solution
def to_underscore(string)
string.to_s.split(/(?=[A-Z])/).join('_').downcase
end
# Another Solution
def to_underscore(string)
string.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase
end