问题我有一个我想使用的自定义划分和征服数组排序器。这一切都运行良好,直到我尝试在我的控制器中的数组上使用它我收到此消息.. NoMethodError (undefined method '<=' for #<Entry:0x0000000ac7d850>)
:任何帮助将不胜感激,谢谢!
这是我的条目模型,我在控制器中调用mergesort方法。
def self.mergesort(container)
return container if (container.size <= 1)
mid = container.size / 2
left = container[0...mid]
right = container[mid...container.size]
merge(mergesort(left), mergesort(right))
end
def self.merge(left, right)
sorted = []
until left.empty? or right.empty?
(left.first <= right.first) ? sorted << left.shift : sorted << right.shift
end
sorted + left + right
end
这是我的Entry控制器,我试图调用它。
def pending_sort
@ent_sort = Entry.where("section = ? and approve_disapprove = ?", @mgr_section, '3')
@ent = Entry.mergesort(@ent_sort)
end
答案 0 :(得分:1)
left
或right
的第一个元素可能有irb(main):001:0> left = []
=> []
irb(main):002:0> right = [1]
=> [1]
irb(main):003:0> left.first
=> nil
irb(main):004:0> left.first <= right.first
NoMethodError: undefined method `<=' for nil:NilClass
from (irb):4
from /usr/bin/irb:11:in `<main>'
。
nil
您可以通过将(left.first <= right.first) ? sorted << left.shift : sorted << right.shift
转换为其他值来修复错误。例如,如果您要比较的值始终为整数,则可以更改以下行:
(left.first.to_i <= right.first.to_i) ? sorted << left.shift : sorted << right.shift
到此:
this.IpOctetFirst.KeyDown += delegate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab ||
e.Key == Key.Home ||
e.Key == Key.End ||
e.Key == Key.Left ||
e.Key == Key.Right ||
e.Key == Key.Back ||
e.Key == Key.Delete ||
e.Key == Key.Decimal ||
(e.Key >= Key.D0 && e.Key <= Key.D9) ||
(e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) ||
/////////////////////////////////example: having error in this line:
(e.Key == Key.Unknown && e.PlatformKeyCode == 190))
{
if (this.IpOctetFirst.Text.Length == 3 &&
this.IpOctetFirst.SelectedText.Length == 0 &&
e.Key != Key.Tab &&
e.Key != Key.Left &&
e.Key != Key.Right &&
e.Key != Key.Decimal &&
(e.Key != Key.Unknown || e.PlatformKeyCode != 190))
{
e.Handled = true;
return;
}
switch (e.Key)
{
case Key.Right:
if (this.IpOctetFirst.SelectionStart == this.IpOctetFirst.Text.Length)
{
this.IpOctetSecond.Focus();
this.IpOctetSecond.SelectionStart = 0;
}
break;
case Key.Decimal:
case Key.Unknown:
if (this.IpOctetFirst.SelectionStart == 0)
e.Handled = true;
break;
}
}
else
{
e.Handled = true;
}
};
但想想它会如何影响你的功能......如果它不是你真正想做的事情,它可能会破坏它。