Home>

CODEPREP.

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.

Error message
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.

/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>'
Problem Let's implement a Car class that represents a car.

Implementation

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
Execution example

car = Car.new (50)
p car.speed

=>50

speed = car.time (100)

=>2.0
Source code
Tried

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.
    Class/method definition
    Module # attr_accessor


      

    class Car
       class Car

    Do not continue with two. Another Car class will be created inside the Car class.

      

    def time (distance)
      end

    Align indentation

      

    attr_accessor @ speed, @ time
         print speed
       

    attr_accessor: speed,: timeis correct

    There is noendcorresponding to

    class Car.


    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,classtoend,def~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

    Thank you