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.
- i have a question about ruby yield from the introduction to ruby for those who want to become a book professional
- ruby on rails - about rails unit test code
- ruby - about integration test errors
- ruby on rails: about nomethoderror that occurred when adding a good function
- ruby - i have a question about rails routing errors
- php - about pdf conversion (image quality) of canvas using html2canvas and jspdf
- ruby on rails - about the problem that rails test does not become green in rails tutorial
- ruby on rails - about the code of the adjacency list model implemented in rails
- ruby on rails - about routing errors in devise
- ruby on rails - about bundler of dockerfile when deploying rails application to ec2
- ruby on rails - about rails:
- ruby - about the error when there is no id of the association destination
- ruby - about errors after deploying heroku
- ruby on rails - [rails] about the display position after redirect by pressing like
- ruby - binary data conversion method
- macos (osx) - about image conversion using pdf2image, poppler
- about the basis of basis conversion in python
- ruby on rails 5 - about the structure when creating a blog with rails 5
- about the empty model created by "new" of ruby on rails
- ruby - about the creation period of the matching service
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- python 3x - typeerror: 'method' object is not subscriptable
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - unresolved reference comes out in kotlin