Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ ruby main.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)
- require "./human"
- # インスタンスの作成
- tanaka = Human.new("田中太郎",25,"電車")
- suzuki = Human.new("鈴木次郎",30,"野球")
- sato = Human.new("佐藤花子",20,"映画")
- # メソッドを実行
- tanaka.say
- tanaka.think
- suzuki.say
- suzuki.think
- sato.say
- sato.think
- require "./thinkable"
- class Human < Animal
- include Thinkable
- #オブジェクトで何の変数を使うか設定する
- attr_accessor :hobby
- #インスタンスの値を初期化する特別なメソッド
- def initialize(name,age,hobby)
- self.name = name
- self.age = age
- self.hobby = hobby
- end
- end
- #クラスの定義
- class Animal
- #オブジェクトの変数
- attr_accessor :name, :age
- #メソッド
- def say
- puts "#{self.name}です。#{self.age}歳です。"
- end
- end
- =begin
- def initialize(name,age)
- self.name = name
- self.age = age
- end
- =end
- =begin
- animal = Animal.new('田中太郎', 25)
- animal.say
- =end
- #クラスの定義
- class Animal
- #オブジェクトの変数
- attr_accessor :name, :age
- #メソッド
- def say
- puts "#{self.name}です。#{self.age}歳です。"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement