Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Schism's answer Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackExchange's system seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackExchange's system seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackExchange's system seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

added 9 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackOverflowStackExchange's system seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackOverflow seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackExchange's system seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

added 121 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackOverflow seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few, known places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few, known places, then don't start messing with basic classes.

Schism's answer touches on the important points: use snake_case, implicit return, and use map rather than modifying the string instances in-place with a bang-method (!).

However, if the string contains many kinds of whitespace - linebreaks, tabs; not just " " - it may not work as intended.

You could consider using gsub in order to maintain any and all original whitespace:

def capitalize_words(string)
 string.gsub(/\S+/, &:capitalize)
end

This will capitalize words separated by any kind of whitespace, and preserve that whitespace in the resulting string.

The regular expression will match 1 or more consecutive non-whitespace characters (i.e. it'll match individual words), which are then passed to an implicit block that calls capitalize on the word, replacing it in the string.

E.g.

input = "here's a STRING with\n\ta newline aNd a tab character"
puts capitalize_words(input)

will print

Here's A String With
 A Newline And A Tab Character

(StackOverflow seems to replace the tab character with 4 spaces, but run the code yourself, and it'll remain a \t)

As for monkey patching the String class... up to you. I'd consider it for something like this, but I wouldn't do it immediately. The functionality is generic enough that it'd make sense as an addition to String, but, IMO, the real question is whether its usage is wide-spread enough. If the functionality is only used in a few places, then don't start messing with basic classes.

added 392 characters in body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90
Loading
lang-rb

AltStyle によって変換されたページ (->オリジナル) /