Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. $ ruby main.rb
  2. Traceback (most recent call last):
  3. 3: from main.rb:1:in `<main>'
  4. 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'
  5. 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'
  6. /home/ec2-user/environment/human.rb:3:in `<top (required)>': uninitialized constant Animal (NameError)
  7.  
  8. require "./human"
  9.  
  10. # インスタンスの作成
  11. tanaka = Human.new("田中太郎",25,"電車")
  12. suzuki = Human.new("鈴木次郎",30,"野球")
  13. sato = Human.new("佐藤花子",20,"映画")
  14.  
  15.  
  16. # メソッドを実行
  17. tanaka.say
  18. tanaka.think
  19. suzuki.say
  20. suzuki.think
  21. sato.say
  22. sato.think
  23.  
  24. require "./thinkable"
  25.  
  26. class Human < Animal
  27. include Thinkable
  28.  
  29. #オブジェクトで何の変数を使うか設定する
  30. attr_accessor :hobby
  31.  
  32. #インスタンスの値を初期化する特別なメソッド
  33. def initialize(name,age,hobby)
  34. self.name = name
  35. self.age = age
  36. self.hobby = hobby
  37. end
  38.  
  39. end
  40.  
  41. #クラスの定義
  42. class Animal
  43. #オブジェクトの変数
  44. attr_accessor :name, :age
  45.  
  46. #メソッド
  47. def say
  48. puts "#{self.name}です。#{self.age}歳です。"
  49. end
  50.  
  51. end
  52. =begin
  53. def initialize(name,age)
  54. self.name = name
  55. self.age = age
  56. end
  57. =end
  58.  
  59.  
  60.  
  61. =begin
  62. animal = Animal.new('田中太郎', 25)
  63. animal.say
  64. =end
  65.  
  66. #クラスの定義
  67. class Animal
  68. #オブジェクトの変数
  69. attr_accessor :name, :age
  70.  
  71. #メソッド
  72. def say
  73. puts "#{self.name}です。#{self.age}歳です。"
  74. end
  75.  
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement