ruby - Undefined method 'center' for nilclass -
i have ternary operator outputs student count.
@students.count < 2 ? puts("now have #{@students.count} student").center(75) : puts("now have #{@students.count} students").center(75) i had put string in brackets after puts otherwise come error:
syntax error, unexpected ':', expecting end-of-input however, since have updated code method .center isn't working , receiving error:
`input_students': undefined method `center' nil:nilclass (nomethoderror) how can around brackets?
problem applying call #center result of puts happens nil
@students.count < 2 ? puts("now have #{@students.count} student".center(75)) : puts("now have #{@students.count} students".center(75)) note: if student count 0, message reads "now have 0 student"
if want refactor extract common parts: centering , outputting
message = count < 2 ? 'message 1' : 'message 2' puts message.center(75) but result still result in long line (depending on message) use
message = if count < 2 'message 1' else 'message 2' end puts message.center(75) imho code needs readable not same short. when there lots of statements crammed 1 line.
Comments
Post a Comment