質問

I'm trying to check a condition:

name = gets.chomp  
if name.capitalized == name      
  puts 'some text'
else
  puts name.capitalize + 'some text'

im getting this error in the terminal:

test.rb:6:in <main>': undefined methodcapitalized' for "name":String (NoMethodError)

役に立ちましたか?

解決

name = gets.chomp
if name.capitalize == name
      puts 'some text'
else
      puts name.capitalize + 'some text'
end

他のヒント

If you add the capitalized? method to String:

class String

  def capitalized?
    chars.first == chars.first.upcase
  end

end

You will be able to check if a string is capitalized and use that in your logic.

'Ruby'.capitalized?
 #=> true

EDIT: It seems that you are merely trying to capitalize the string itself and not check. In that case:

'ruby'.capitalize
 #=> 'Ruby'

Should do what you need.

It is String#capitalize.try this.

This is a typo if name.capitalized change it to if name.capitalize

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top