我很新的Ruby編程,我花了很多時間在這個程序,但我來到一個點,我不明白我錯了, m得到,簡單的Ruby編程,出現錯誤,不明白爲什麼,
這將是很好,如果你可以幫助找到錯誤,並可能修復它。
謝謝。
Here is the complete description of program and actual code:
錯誤:
C:/Users/Amir-i7/Dropbox/CS431/hwk9/hwk9.rb:148:in `+': no implicit conversion of Array into String (TypeError)
from C:/Users/Amir-i7/Dropbox/CS431/hwk9/hwk9.rb:148:in `test_tree'
from C:/Users/Amir-i7/Dropbox/CS431/hwk9/hwk9.rb:152:in `<main>'
代碼:
module TreeEnum
def any? f
s = false
self.iterate(f).each do |child|
s = s || child.iterate(f) end
s
end
def inject(f,c)
z = lambda {|data| f.call(data,c)}
iterate z
end
end
class Leaf
def initialize s
@data = s
end
def concatAll
@data
end
def firstAlphabetical
@data
end
def iterate(itr)
itr.call(@data)
end
end
class BinaryNode
include TreeEnum
def initialize (left, right)
@left = left
@right = right
end
def concatAll
@left.concatAll + @right.concatAll
end
# not part of the homework
def BinaryNode.firstAlphabetical (s1, s2)
if s1.casecmp(s2) < 0 then s1 else s2 end
end
def firstAlphabetical
s1 = @left.firstAlphabetical
s2 = @right.firstAlphabetical
if s1.casecmp(s2) < 0 then s1 else s2 end
end
def iterate itr
@left.iterate itr
@right.iterate itr
end
def BinaryNode.concatAll tree
s = "";
tree.iterate(lambda { |data| s = s + data })
s
end
end
class NaryNode
include TreeEnum
def initialize childArray
@childArray = childArray.clone
end
def iterate itr
# use the "each" method of array to pass "itr" to the iterate method of each element in @childArray
@childArray.each do |child|
child.iterate itr
end
end
def concatAll
# use the "inject" method of array to concatenate the strings of each node in @childArray
s = ""
@childArray.each do |child|
s = s + child.concatAll
end
s
end
def firstAlphabetical
# use the "inject" method of array to retrieve the smallest string of the nodes in @childArray
@childArray.inject(@childArray[0]) {|first, node| if first.firstAlphabetical.casecmp(node.firstAlphabetical) < 0 then first.firstAlphabetical else node.firstAlphabetical end}
end
end
class String
def iterate itr
itr.call(self)
end
def concatAll
self
end
def firstAlphabetical
self
end
end
def test_print t2
puts "t2.concatAll: " + t2.concatAll.to_s
puts
puts "t2.firstAlphabetical: " + t2.firstAlphabetical.to_s
puts
puts "t2.iterate(lambda { |s| puts s }):"
t2.iterate(lambda { |s| puts s })
end
def test_tree
l0 = Leaf.new "What "
l1 = Leaf.new "a "
l2 = Leaf.new "great "
l3 = Leaf.new "day"
l4 = Leaf.new "!"
t0 = BinaryNode.new(l0,l1)
t1 = BinaryNode.new(t0,l2)
t2 = NaryNode.new([t1,l3,l4])
test_print t2
puts "\nThe following works after question 2\n\n"
t2 = NaryNode.new([t1, "day", "!"])
test_print t2
puts "\nThe following works after question 3\n\n"
puts "any word starting with `great': " + t2.any?(lambda {|x| x.start_with?("great")}).to_s
puts
puts "capitalize: " + t2.inject((lambda {|acc, elm| acc + elm.capitalize + " "}), "")
end
test_tree
我已經嘗試過用
puts "capitalize: " + t2.inject((lambda {|acc, elm| acc + elm.capitalize + " "}), "").to_s
更換管線
,但我得到:
capitalize: [#<BinaryNode:0x00000002ce5958 @left=#<BinaryNode:0x00000002ce5980 @left=#<Leaf:0x00000002ce5ae8 @data="What ">, @right=#<Leaf:0x00000002ce5a98 @data="a ">>, @right=#<Leaf:0x00000002ce5a48 @data="great ">>, "day", "!"]
這將是很高興知道什麼在行148.無論如何,看起來像你試圖連接一個數組與字符串... – aldux
如果你能提供幫助和可能的解決方案,那將是非常好的,記住我不能改變測試部分。 –
這是兩個不同的問題,你想如何解決後者? – DiegoSalazar