Understanding Ruby Modularization

Asked 2 years ago, Updated 2 years ago, 44 views

We are solving the problem of modularizing ruby.
If you have trouble solving the error statement and you know the solution, please let me know.


when run in rubymain.rb The error description is as follows.

$rubymain.rb
Traceback (most recent call last):
        3:from main.rb:1:in`<main>'
        2:from/home/ec2-user/.rvm/rubies/ruby-2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in`require'
        1:from/home/ec2-user/.rvm/rubies/ruby-2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in`require'
/home/ec2-user/environment/human.rb:3:in`<top(required)>':uninitialized constant Animal(NameError)

main.rb

require"./human"

# Creating an Instance
tanaka=Human.new ("Taro Tanaka", 25, "Train")
suzuki=Human.new ("Jiro Suzuki", 30, "baseball")
sato=Human.new ("Sato Hanako", 20, "Movie")

# Run Method
tanaka.say
tanaka.think
suzuki.say
suzuki.think
sato.say
sato.think

human.rb

require"./thinkable"

class Human<Animal
    include Thinkable

    # set what variables to use in an object
    attr_accessor:hobby

    # Special Method for Initializing Instance Values
    default initialize (name, age, hobby)
        self.name = name
        self.age=age
        self.hobby=hobby
    end

end

animal.rb

#Defining a Class
class Animal
# Object Variables
    attr_accessor:name, :age

# method
def say
    puts"#{self.name}.# {self.age} years old."
end 

end
= begin
   default initialize(name,age)
    self.name = name
    self.age=age
  end
=end

= begin
animal=Animal.new ('Taro Tanaka', 25)  
animal.say
=end

thinkable.rb

#Defining a Class
class Animal
# Object Variables
    attr_accessor:name, :age

# method
def say
    puts"#{self.name}.# {self.age} years old."
end 

end

ruby-on-rails ruby

2022-09-30 14:50

1 Answers

The human.rb requires require"./animal".


2022-09-30 14:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.