Use only the first character of the string and leave others alone? (Rails)

I am trying to get Rails to use the first character of the string and leave everyone else as they are. I run into a problem when "I'm from New York" turns into "I'm from New York."

What method would I use to select the first character?

thank

EDIT: I tried to implement what the matzek suggested, but I get an error "

def fixlistname! self.title = self.title.lstrip + (title.ends_with?("...") ? "" : "...") self.title[0] = self.title[0].capitalize errors.add_to_base("Title must start with \"You know you...\"") unless self.title.starts_with? 'You know you' end 

EDIT 2:. He works. Thanks for the help!

EDIT 3: Wait, no, I didn’t ... This is what I have in my list model.

 def fixlistname! self.title = self.title.lstrip + (title.ends_with?("...") ? "" : "...") self.title.slice(0,1).capitalize + self.title.slice(1..-1) errors.add_to_base("Title must start with \"You know you...\"") unless self.title.starts_with? 'You know you' end 

EDIT 4: Fixed editing macek and still getting the `w91>` capize 'error. What can i do wrong?

 def fixlistname! self.title = title.lstrip self.title += '...' unless title.ends_with?('...') self.title[0] = title[0].capitalize errors.add_to_base('Title must start with "You know you..."') unless title.starts_with?("You know you") end 

EDIT 5: This is strange. I can get rid of an undefined method error using the line below. The problem is that it seems to replace the first letter with a number. For example, instead of smoothing y into You, it turns y into 121

 self.title[0] = title[0].to_s.capitalize 
+97
string ruby-on-rails capitalization
Apr 15 2018-10-15T00:
source share
17 answers

Titleize will use every word. This line feels great, but guarantees that the only letter is changed - this is the first.

 new_string = string.slice(0,1).capitalize + string.slice(1..-1) 

Update:

 irb(main):001:0> string = "i'm from New York..." => "i'm from New York..." irb(main):002:0> new_string = string.slice(0,1).capitalize + string.slice(1..-1) => "I'm from New York..." 
+91
Apr 15 '10 at 16:13
source share

This should do it:

 title = "test test" title[0] = title[0].capitalize puts title # "Test test" 
+120
Aug 22 2018-11-18T00:
source share

You can use humanization. Unless you need underscores or other capitals in text strings.

Input:

 "i'm from New_York...".humanize 

Output:

 "I'm from new york..." 
+55
Jun 28 2018-12-12T00:
source share
 str = "this is a Test" str.sub(/^./, &:upcase) # => "This is a Test" 
+43
Mar 15 '13 at 19:23
source share

As Rails 5.0.0.beta4, you can use the new String#upcase_first or ActiveSupport::Inflector#upcase_first for this. See the blog post for more details.

+31
May 20 '16 at 1:37
source share

Object Oriented Solution:

 class String def capitalize_first_char self.sub(/^(.)/) { $1.capitalize } end end 

Then you can simply do this:

 "i'm from New York".capitalize_first_char 
+14
Sep 14
source share
 str.sub(/./, &:capitalize) 
+7
Feb 09 '16 at 18:50
source share

Edit 2

I can not repeat your problem. Go ahead and run this native Ruby script. It generates the exact result you are looking for, and Rails supports all of these methods. What resources do you encounter?

 #!/usr/bin/ruby def fixlistname(title) title = title.lstrip title += '...' unless title =~ /\.{3}$/ title[0] = title[0].capitalize raise 'Title must start with "You know you..."' unless title =~ /^You know you/ title end DATA.each do |title| puts fixlistname(title) end __END__ you know you something WITH dots ... you know you something WITHOUT the dots you know you something with LEADING whitespace... you know you something with whitespace BUT NO DOTS this generates error because it doesn't start with you know you 

Exit

 You know you something WITH dots ... You know you something WITHOUT the dots... You know you something with LEADING whitespace... You know you something with whitespace BUT NO DOTS... RuntimeError: Title must start with "You know you..." 



Edit

Based on your editing, you can try something like this.

 def fixlistname! self.title = title.lstrip self.title += '...' unless title.ends_with?('...') self.title[0] = title[0].capitalize errors.add_to_base('Title must start with "You know you..."') unless title.starts_with?("You know you") end 



Original

It will do the trick

 s = "i'm from New York" s[0] = s[0].capitalize #=> I'm from New York 



When you try to use String#capitalize for the entire string, you see I'm from new york , because the method:

Returns a copy of str with the first character converted to uppercase, and the rest to lowercase.

 "hello".capitalize #=> "Hello" "HELLO".capitalize #=> "Hello" "123ABC".capitalize #=> "123abc" 
+6
Apr 15 2018-10-15T00:
source share
 my_string = "hello, World" my_string.sub(/\S/, &:upcase) # => "Hello, World" 
+6
May 26 '14 at 2:06
source share

Most of these answers edit the line in place, when you just format the output for presentation, you may not want to change the base line so you can use tap after dup to get the edited copy

 'test'.dup.tap { |string| string[0] = string[0].upcase } 
+4
Jan 02 '13 at 13:26
source share

If and only if the OP wants to execute a monkey patch on a String object, then this can be used

 class String # Only capitalize first letter of a string def capitalize_first self.sub(/\S/, &:upcase) end end 

Now use it:

 "i live in New York".capitalize_first #=> I live in New York 
+3
Nov 13 '15 at 8:07
source share

No one mentions gsub which allows you to do this briefly.

 string.gsub(/^([az])/) { $1.capitalize } 

Example:

  > 'caps lock must go'.gsub(/^(.)/) { $1.capitalize } => "Caps lock must go" 
+2
Jun 05 2018-12-12T00
source share

An even shorter version could be:

 s = "i'm from New York..." s[0] = s.capitalize[0] 
+1
Dec 09 '11 at 7:52
source share

Note that if you need to deal with multibyte characters, i.e. If you want to internationalize your site, the solution s[0] =... will not be suitable. This question suggests using the unicode-util gem

Ruby 1.9: how to correctly register and iterate over multibyte strings?

EDIT

Actually an easier way to at least avoid weird string encoding is to simply use String # mb_chars :

 s = s.mb_chars s[0] = s.first.upcase s.to_s 
+1
May 7 '12 at 22:17
source share

Perhaps the easiest way.

 s = "test string" s[0] = s[0].upcase # => "Test string" 
+1
09 Oct '13 at 9:19
source share

How about classifying a method in a string?

 'somESTRIng'.classify 

exit:

 #rails => 'SomESTRIng' 
0
May 15 '14 at 11:40
source share
 string = "i'm from New York" string.split(/\s+/).each{ |word,i| word.capitalize! unless i > 0 }.join(' ') # => I'm from New York 
-3
Apr 15 2018-10-15T00:
source share



All Articles