2014-07-15 3 views
0

나는 세 NetAddr :: CIDR 객체의 배열을 가지고 있고 NetAddr 모듈의 cidr_sort 방법을 사용하여 정렬을 시도하고 (http://rubydoc.info/gems/netaddr/1.5.0/NetAddr#cidr_sort-class_method)루비 "#에 대한 정의되지 않은 방법 '길이'<NetAddr :: CIDRv4 ..."

Class IPv4SummaryNet 
    attr_accessor :component_nets 
    @component_nets = [] 

    def add_net(net) 
     @component_nets = component_nets.to_a.push(net) 
    end 

    def sort_component_nets 
     component_nets_sorted = @component_nets.sort 
    end 
    ... 
end 

내가받을 다음과 같은 오류 : 나는 내 프로그램 내에서 배열의 길이를 인쇄 할 경우에, 나는 /usr/local/lib/ruby/gems/2.1.0/gems/netaddr-1.5.0/lib/cidr_shortcuts.rb:216:in 'cidr_sort': undefined method 'length' for #<NetAddr::CIDRv4:0x007f55cbae0088> (NoMethodError)

를 얻을 CORR를 다음과 같이 내 클래스 내에서 sort 메소드를 호출

ect 값이 3입니다.

또한 sort_byNetAddr::cidr_sort(@component_nets)을 사용해도 동일한 오류가 발생합니다.

cidr_sort 메서드가 호출하려고 시도했을 때 Ruby에서 나에게 말하는 length의 이유는 무엇입니까?하지만 문제없이 내 코드에서 호출 할 수 있습니까?

+0

오류는'Array'에서 누락 된 방법에 대한이 아니라'NetAddr :: CIDRv4' –

+0

의 인스턴스에서 나는 이해가 확실하지 않다. '@ component_nets'는'cidr_sort' 메쏘드에 전달되는'NetAddr :: CIDRv4' 객체의 배열입니다. 'cidr_sort'는'array'에'length' 메쏘드를 호출합니다 – Ben

+0

분명히 아닙니다 - 여러분이 생각한 것을 전달하지 않거나 메서드가 여러분이 생각하는 것을하지 않습니다 ... –

답변

0
def sort_component_nets 
     component_nets_sorted = @component_nets.sort 
    end 

여기에 루비는 메서드 블록 내에서 지역 변수를 만들려고,하지만 난 당신이 실제로이 방법은 그 블록 밖에 component_nets_sorted에 액세스하려는 경우를 가정 해 보겠습니다 생각합니다. 이렇게하려면 자기를 추가해야합니다. 접두사.

def sort_component_nets 
     self.component_nets_sorted = @component_nets.sort 
    end 
+0

메서드 블록 내에서 로컬 변수를 만들려고합니다. – Ben

관련 문제