name = gets.chomp
if name.capitalize == name
puts 'some text'
else
puts name.capitalize + 'some text'
end
Can't check if Ruby object is capitalised
-
04-06-2022 - |
質問
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 method
capitalized' for "name":String (NoMethodError)
解決
他のヒント
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
所属していません StackOverflow