Ruby 如何使类对象返回数组而不是<;类对象:0x007fc94a0…]>;?
!!!!我正在清理我的代码,重新思考我的问题。我将在几分钟内重新发布并编辑该版本。谢谢你的回复 这是我的密码:Ruby 如何使类对象返回数组而不是<;类对象:0x007fc94a0…]>;?,ruby,arrays,class,initialization,Ruby,Arrays,Class,Initialization,!!!!我正在清理我的代码,重新思考我的问题。我将在几分钟内重新发布并编辑该版本。谢谢你的回复 这是我的密码: class Student attr_accessor :scores, :first_name def initialize(first_name, scores) @first_name = first_name @scores = scores end def average @scores.inject {|sum, el|
class Student
attr_accessor :scores, :first_name
def initialize(first_name, scores)
@first_name = first_name
@scores = scores
end
def average
@scores.inject {|sum, el| sum + el }.to_f / @scores.size
end
def letter_grade
case average
when (90..100)
"A"
when (80..89)
"B"
when (70..79)
"C"
when (60..69)
"D"
when (0..59)
"F"
end
end
end
me = Student.new("Alex", [100,100,100,0,100])
student_2 = Student.new('Hodor', [2,2,7,0,90])
student_3 = Student.new('Aria', [90,100,87,90,90])
student_4 = Student.new('Tyrion', [95,100,97,100,30])
student_5 = Student.new('Jaela', [100,100,100,100,100])
students = [me, student_2, student_3, student_4, student_5]
p students
以下是我得到的反馈:
[#<Student:0x007f92a91e6070 @first_name="Alex", @scores=[100, 100, 100, 0, 100]>, #<Student:0x007f92a91e5ff8 @first_name="Hodor", @scores=[2, 2, 7, 0, 90]>, #<Student:0x007f92a91e5f80 @first_name="Aria", @scores=[90, 100, 87, 90, 90]>, #<Student:0x007f92a91e5f08 @first_name="Tyrion", @scores=[95, 100, 97, 100, 30]>, #<Student:0x007f92a91e5e90 @first_name="Jaela", @scores=[100, 100, 100, 100, 100]>]
所以我想,我真的需要在学生课堂上这样做 我不确定这个练习的目的是什么,但要从实际输出到预期输出,您只需检查元素,然后用每个元素构建一个数组(使用):
如果您试图输出Student实例,ruby将调用Student实例上的_s()。如果类不提供to_s()方法,则调用继承的to_s()方法(类内对象),该方法提供您看到的字符串。如果将对象重新定义为,则可以证明:
#Your code here
class Object
def to_s
'hello from Object#to_s'
end
end
p students
--output:--
[hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s]
如果在Student中重写to_s()方法,则无论何时尝试输出Student对象,ruby都将调用该方法并使用其返回值:
require 'pp'
class Student
attr_accessor :scores, :first_name
...
def to_s
"#{first_name} #{scores.inspect}"
end
end
students = [
Student.new("Alex", [100,100,100,0,100]),
Student.new('Hodor', [2,2,7,0,90]),
Student.new('Aria', [90,100,87,90,90]),
Student.new('Tyrion', [95,100,97,100,30]),
Student.new('Jaela', [100,100,100,100,100]),
]
pp students
--output:--
[Alex [100, 100, 100, 0, 100],
Hodor [2, 2, 7, 0, 90],
Aria [90, 100, 87, 90, 90],
Tyrion [95, 100, 97, 100, 30],
Jaela [100, 100, 100, 100, 100]]
在代码片段scores.inspect
中,inspect()方法是p
使用的方法,即p scores
相当于print scores.inspect+“\n”
。但你不能写:
"some string #{p.scores}"
因为字符串插值使用返回值
p.scores
,p
,就像put
,总是返回零。你想做什么?目标是能够按名称搜索学生数组。我在上面添加了我需要通过的测试和搜索功能。这是学校的一项评估挑战。你可以将@scores.inject{sum,el | sum+el}
写成@scores.inject(:+)
。检查文档(akareduce
)。我终于弄明白了!我用过。地图。谢谢你。
require 'pp'
class Student
attr_accessor :scores, :first_name
...
def to_s
"#{first_name} #{scores.inspect}"
end
end
students = [
Student.new("Alex", [100,100,100,0,100]),
Student.new('Hodor', [2,2,7,0,90]),
Student.new('Aria', [90,100,87,90,90]),
Student.new('Tyrion', [95,100,97,100,30]),
Student.new('Jaela', [100,100,100,100,100]),
]
pp students
--output:--
[Alex [100, 100, 100, 0, 100],
Hodor [2, 2, 7, 0, 90],
Aria [90, 100, 87, 90, 90],
Tyrion [95, 100, 97, 100, 30],
Jaela [100, 100, 100, 100, 100]]
"some string #{p.scores}"