0

Am trying to loop through a string which i have converted to an array and target only the upcase letters which i will then insert an empty space before the capitalized letter. My code checks for the first cap letter and adds the space but am struggling to do it for the next cap letter which in this case is "T". Any advise would be appreciated. Thanks

  def break_camel(str)
  # ([A-Z])/.match(str)
  saved_string = str.freeze
  cap_index =str.index(/[A-Z]/)
  puts(cap_index)

   x =str.split('').insert(cap_index, " ")
  x.join

end
break_camel("camelCasingTest")
  • Can you put the desired output for your example. – Sagar Pandya Apr 08 '20 at 02:17
  • I am assuming that you are mainly concerned with how best to modify your string, rather than how best to do that by adopting a particular approach: converting it to an array, operating on the elements of the array and then rejoining the pieces. If so, I suggest you state problems in a way that specifies only the end result you want, making no assumptions about the approach that should be taken. Becoming fixated with a certain approach is sometimes called the [X-Y Problem](https://en.wikipedia.org/wiki/XY_problem), something to guard against. – Cary Swoveland Apr 08 '20 at 04:57

3 Answers3

1

I think your approach is looking to keep reapplying your method until needed. One extension of your code is to use recursion:

def break_camel(str)
  regex = /[a-z][A-Z]/
  if str.match(regex)
    cap_index = str.index(regex)
    str.insert(cap_index + 1, " ")
    break_camel(str)
  else
    str  
  end
end

break_camel("camelCasingTest") #=> "camel Casing Test"

Notice the break_camel method inside the method. Another way is by using the scan method passing the appropriate regex before rejoining them.

In code:

'camelCasingTest'.scan(/[A-Z]?[a-z]+/).join(' ') #=> "camel Casing Test"
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
1

It's much easier to operate on your string directly, using String#gsub, than breaking it into pieces, operating on each piece then gluing everything back together again.

def break_camel(str)
  str.gsub(/(?=[A-Z])/, ' ')
end

break_camel("camelCasingTest")
  #=> "camel Casing Test"
break_camel("CamelCasingTest")
  #=> " Camel Casing Test"

This converts a "zero-width position", immediately before each capital letter (and after the preceding character, if there is one), to a space. The expression (?=[A-Z]) is called a positive lookahead.

If you don't want to insert a space if the capital letter is at the beginning of a line, change the method as follows.

def break_camel(str)
  str.gsub(/(?<=.)(?=[A-Z])/, ' ')
end

break_camel("CamelCasingTest")
  #=> "Camel Casing Test"

(?<=.) is a positive lookbehind that requires the capital letter to be preceded by any character for the match to be made.

Another way of writing this is as follows.

def break_camel(str)
  str.gsub(/(?<=.)([A-Z]))/, ' \1')
end

break_camel("CamelCasingTest")
  #=> "Camel Casing Test"

Here the regular expression matches a capital letter that is not at the beginning of the line and saves it to capture group 1. It is then replaced by a space followed by the contents of capture group 1.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

Do you have to implement your own? Looks like titleize https://apidock.com/rails/ActiveSupport/Inflector/titleize has this covered.

Joel Blum
  • 7,750
  • 10
  • 41
  • 60
  • its basically a task i have been set at my bootcamp. I do have to show my own working method. Thanks – Fabio Andres Apr 07 '20 at 23:35
  • This should be a comment, but I don't think `titleize` is suitable, as `titleize("camelCasingText") #=> "Camel Casing Text"` and `titleize("cat") #=> "Cat"`, whereas the OP wishes to return `"camel Case Text"` and `"cat"`, respectively. – Cary Swoveland Apr 08 '20 at 05:05