I don't understand the object type conversion specificationError message
An error will occur if the string is converted to a number. .
Why can I do it when I enter the formula?
String can't be coerced into Fixnum (TypeError)
x = 50
y = "3"
p x + y
x = 50
y = "3"
p x + y.to_i
This worked without problems
Please describe what you tried for the problem here.
Answer # 1
y.to_i, you may have intended to convert the object pointed to by
yfrom String to Integer, but the Integer object is discarded as it is. If you want to refer to that Integer by
y, assign it to
When you write
x = 50 y = "3" y = y.to_i p x + y
variable.method, the object pointed to by the variable will not change to an object of another class. The value may change while the object is the same class.
Answer # 2
ruby's type conversion does not have the function of automatically converting integer + character string
Because x + y is in that form, it will result in an error that the type is different
If it is x + y.to_i, it will pass because y is converted to an integer with y.to_i
Automatic type conversion is performed for operations such as integers and real numbers whose types differ between numbers.
- ruby on rails 6 - about validation (number of characters) of rails tweet posting
- ruby on rails - about screen transition from rails top page to calendar page
- ruby - about the problem of duplicate devise error messages (allow_blank: ture doesn't work!)
- ruby - about the error content displayed in "rails" rspec
- ruby on rails - about image posting page argumenterror
- ruby on rails - rails render: about the matter that the url after new is different
- ruby - [rails] about partial templates
- python - about unhashable, object is not callable
- [ruby on rails] about header setting using bootstrap
- mql4 - about object create
- ruby - about accelerating heroku's rails app
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"
- ruby on rails - about rails create action
- ruby - about record duplication
- ruby on rails - about nomethoderror in skilscontroller # create
- about ruby exception method stackrace
- ruby - about rails controller new action
- ruby - about jquery cookies
- ruby on rails - about request spec of rspec about test code at login
- ruby on rails 5 - about lint/syntax error of rubocop