views:

607

answers:

3

Hi,

I'm trying to get Rails to capitalize the first character of a string, and leave all the others the way they are. I'm running into a problem where "i'm from New York" gets turned into "I'm from new york."

What method would I use to select the first character?

Thanks

EDIT: I tried to implement what macek suggested, but I'm getting a "undefined method `capitalize'" error. The code works fine without the capitalize line. Thanks for the help!

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: Got it working. Thanks for the help!

EDIT 3: Wait, no I didn't... Here's 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: Tried macek's edit, and still getting an undefined method `capitalize'" error. What could I be doing 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 weird. I'm able to get rid of the undefined method error by using the line below. The problem is that it seems to replace the first letter with a number. For example, instead of capitalizing the y in You, it turns the y into a 121

self.title[0] = title[0].to_s.capitalize
A: 
string = "i'm from New York"
string.split(/\s+/).each{ |word,i| word.capitalize! unless i > 0 }.join(' ')
# => I'm from New York
Jeriko
holy overkill, batman!
macek
Seems so. I'm new to ruby, and the other answers didn't actually do what the OP asked, so I made it work :DCorrect me if I'm wrong, but AFAIK modifying a string by changing string[i] doesn't work in a lot of languages?
Jeriko
@Jeriko, this is a Ruby-specific question. It doesn't matter if `string[i]` doesn't work in other languages. Please help keep StackOverflow clutter-free of these kind of hacked-up answers. We can't say RTFM, but even a *quick* glance at the `String` docs would've helped avoid an answer like this...
macek
Haha, thanks for helping though!
Daniel O'Connor
+2  A: 

Edit 2

I can't seem to replicate your trouble. Go ahead and run this native Ruby script. It generates the exact output your looking for, and Rails supports all of these methods. What sort of inputs are you having trouble with?

#!/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

output

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 edit, 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

This will do the trick

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

When trying to use String#capitalize on the whole string, you were seeing I'm from new york because the method:

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

"hello".capitalize    #=> "Hello"
"HELLO".capitalize    #=> "Hello"
"123ABC".capitalize   #=> "123abc"
macek
+1 cause I didn't think that would work till I tried it on my pc :)
Jeriko
Hi, thanks for the help. What am I doing wrong though? I've edited the original question to include my code.
Daniel O'Connor
Now the code is actually up :)
Daniel O'Connor
I used Taryn's suggestion and got it working. Thanks for the help though!
Daniel O'Connor
I've made two more edits to my code, but still no luck...
Daniel O'Connor
Hi, stackoverflow says you made an edit, but I don't see anything different?
Daniel O'Connor
A: 

Titleize will capitalise every word. This line feels hefty, but will guarantee that the only letter changed is the first one.

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..."
Taryn East
Worked perfectly. Thanks!
Daniel O'Connor
Wait, still not working. False alarm :/I think I'm just implementing it wrong.
Daniel O'Connor
what's actually going wrong for you?
Taryn East