Please write your question in detail here.
(Example) I am making a ●● system with PHP (CakePHP).
The following error message occurred while implementing the ■■ function.
class Car class Car def initialize (speed) @speed = speed km = @ speed/time end def time (distance) end attr_accessor @ speed, @ time print speed car = Car.new (50) p car.speed speed = car.time (100)
An error has occurred.
Problem Let's implement a Car class that represents a car.
/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require ': /root/codecheck/main.rb:18: syntax error, unexpected end-of -input, expecting keyword_end (SyntaxError) speed = car.time (100) ^ from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require ' from test.rb: 1: in `<main>'
The Car class receives the argument speed when initializing the instance object, and sets the value to the instance variable speed
The instance variable speed is a variable that represents the vehicle speed (km/h).
Make sure that the instance variable speed can be referenced from outside the class.
It has a time method that returns the required time when the distance (km) is passed as an instance method
The time method should return nil when the instance variable speed is 0
car = Car.new (50)
speed = car.time (100)=>2.0
Please describe what you tried for the problem here.Supplemental information (FW/tool version etc.)
Please provide more detailed information here.
Answer # 1
I recommend reading the official reference first.
Module # attr_accessor
Do not continue with two. Another Car class will be created inside the Car class.
def time (distance)
attr_accessor @ speed, @ time
There is no
attr_accessor: speed,: timeis correct
class Car attr_accessor: speed def initialize (speed) self.speed = speed end def time (distance) return nil if speed == 0 distance.fdiv speed end end car = Car.new (50) p car.speed p car.time (100)
Answer # 2
Before class design,
endnesting is not supported. Try to clean the nest and rewrite it.
class Car class Car def initialize (speed) @speed = speed km = @ speed/time end def time (distance) end attr_accessor @ speed, @ time print speed car = Car.new (50) p car.speed speed = car.time (100) ## There is no end to close the class!
Answer # 3
I wrote it.
class Car attr_reader: speed def initialize (speed) raise ArgumentError, 'speed<0' if speed<0 @speed = speed end def time (distance) raise ArgumentError, 'distance<0' if distance<0 return nil if @ speed.zero? distance.to_f/@speed end end car = Car.new (50) p car p car.speed p car.time (100) # =>2.0 car = Car.new (0) p car.time (100) # =>nil begin Car.new (-1) rescue ArgumentError =>ex p ex end
Neither spped nor distance can be set less than 0.
Answer # 4
- ruby - i want to define a human class
- ruby on rails 5 - i don't understand the difference between signup_path and new_user_path
- ruby on rails - i don't understand why the server starts in the background
- Understand the factory pattern of JAVA design pattern in one article
- ruby - rails tutorial 13 chapter 1329 i can't understand the cause because the test cannot be passed
- ruby - inquiry form table design
- ruby - cases where you can not understand the behavior of const_missing well
- ruby - about the contents of the object-oriented design practice guide ~ reasons for using attr_reader
- i want to use exit outside the ruby class to exit the method processing
- ruby on rails 5 - i don't understand the syntax of select_tag
- i don't know ruby class name new(:symbol)
- ruby - please advise on the specification design of rails ec site
- ruby on rails 5 - i can't understand the difference between helper and partial
- ruby on rails 5 - i do not understand the meaning of self in the model
- ruby on rails 5 - i do not understand the code that returns the hash value of the passed string
- java - i don't understand the method call inside the class type method
- ruby - i do not understand the email sending settings for devise
- ruby on rails 5 - i don't understand why i create a virtual attribute
- ruby - i don't know where to write class in select tag
- i want to understand python class __init__ and self
- 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
- 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
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture