I have a question about the Rruby class.Create ruby code below
puts '===================================================='
puts '1 class creation, instance method, getter, setter'
puts '===================================================='
class Warrior
@@class_hensu
@class_instance_hensu
default initialize (name:, hp:, ap:)
puts 'php constructor'
@name = name
@ hp = hp
@ap = ap
puts"#{@name}:#{@hp}:#{@ap}"
puts@mirenai
p@nildesu
puts 'initialize terminated'
end
# instance method
default attack
a = 'Attack!'
put a
puts "#{@name} caused damage of #{@ap}"
end
# getter method
def name
@name
end
# setter method
def name=(name)
@name = name
end
# Getter setter can be created automatically with attr_accessor
attr_accessor:hp
# with the getter and setter omitted
attr_reader:ap
attr_writer —ap
end
yamada=Warrior.new(name: 'Yamada', hp:1000, ap:1000)
yamada.attack
yamada.name = 'Sato'
yamada.attack
puts
puts '============================='
puts '2 Class Integration'
puts '============================='
class Warrior
def call
voice = 'Let's go hard!'
puts voice
end
end
Yamada.call
puts
puts
puts '=========================='
puts '3 class method'
puts '=========================='
class Warrior
def self.run
Puts 'Running off to the prairie'
end
class<<self
def create_warriors (warriors)
warriors.map do|warriors|
Warrior.new (warrior[:name], warrior[:hp], warrior[:ap])
end
end
end
end
Warrior.run
warriors_info = [
{name:'Yamada', hp:1000, ap:1000},
{name:'Sato', hp:500, ap:500}
]
warriors=Warriors.create_warriors(warriors_info)
warriors.each do|warriors |
puts warrior.attack
end
Write the above code at the terminal.
====================================================
1 Class Creation, Instance Method, Getter, Setter
====================================================
constructor in php
Yamada: 1000:1000
nil
initialize end
Attack!
Yamada did 1000 damage.
Attack!
The sugar did 1000 damage of a thousand.
=============================
2 Class Integration
=============================
Let's go like crazy!
==========================
three-class method
==========================
I drove off to the prairie.
Traceback (most recent call last):
5:from sample06.rb:102:in`<main>'
4:from sample06.rb:88:in`create_warriors'
3:from sample06.rb:88:in `map'
2:from sample06.rb:89:in`block in create_warriors'
1:from sample06.rb:89:in`new'
sample06.rb: 14: in `initialize': wrong number of arguments (given3, expected 0; required keywords: name, hp, ap) (ArgumentError)
appears.Probably
class<<self
def create_warriors (warriors)
warriors.map do|warriors|
Warrior.new (warrior[:name], warrior[:hp], warrior[:ap])
end
end
end
Or
warriors_info=[
{name:'Yamada', hp:1000, ap:1000},
{name:'Sato', hp:500, ap:500}
]
I thought there might be a problem with how to pass the arguments in the section, so I looked into it, but I couldn't find a way to improve it.
I would appreciate it if you could give me some advice as I am stuck in this situation.Thank you for your cooperation.
As you can guess, this is a question of how to pass arguments to the new method.
warriors.map do|warrior|
Warrior.new (warrior[:name], warrior[:hp], warrior[:ap])
end
If you do the following, it should work.
warriors.map do|warrior|
Warrior.new(name:warrior[:name], hp:warrior[:hp], ap:warrior[:ap])
end
Also, if you use ruby's function of "automatically converting hash to keyword argument", you can make the following calls:
warriors.map do|warrior|
Warrior.new(warrior)
end
However, please note that this "automatic conversion of hash to keyword argument" feature is deprecated from Ruby 2.7.
© 2024 OneMinuteCode. All rights reserved.