Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTERS.csv
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@
12,Vatsal Narwal,VatsalNarwal,0
84,Vritika Malhotra,VritikaMalhotra,1
86, Safney, safney,0
100, Debora Fernandes, debboraefernandess, 0

50 changes: 50 additions & 0 deletions Ruby/parse_string_to_var_or_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Methods definitions

def transform(text, choice)
case choice
when 1
puts to_camel_case(text)
when 2
puts classify(text)
else
puts text
end
end

# This works for:
# String -> string
# StringString -> string_string
# String String -> string_string
# String 0 StriNG -> string_0_string
def to_camel_case(text)
text.downcase.gsub(" ", "_")
end

# This works for:
# string -> String
# string_string -> StringString
# string String -> StringString
# string 0 string -> String0String
def classify(text)
words = text.gsub(/[\s]+/, "_").split("_")

words.map(&:to_s).map(&:capitalize!)

words.join
end

puts "Give us a string to normalize"
text = gets.chomp

choices = <<STR

What do you want to do? ( pic a number)
- [1] Transform to camel_case
- [2] Transform to class name
STR

puts choices
choice = gets.chomp.to_i

transform(text, choice)