2014-11-08 8 views
0

나는 모듈에 의해 extend을 내 lib - 폴더에서 클래스 Formular을 만들었습니다, 그 또한 내 lib - 폴더에 할당 : 동일한 코드를 넣을 위치는 어디입니까? 믹스 인?

class Formular 
    extend Userfield 
    def self.ueberweisung 
    .................. 
    pdf = Prawn::Document.new(:page_size => [width,height], :margin => 0) 
    pdf.image image_path, :at => [w, height], :fit => [width,height] 

    create_userfield 

    pdf.stroke_line [w + 9.1.mm, h - 60.mm], [w + 14.2.mm, h - 65.mm] 
    .................. 
formular.rb :

그것은 다음과 같이 보입니다

create_personenfeld

module Userfield 
    def create_userfield 
     pdf.font("Courier", :size => 12) do 
     pdf.draw_text "Thanks for your help", :at => [w + 11.mm, h - 13.mm] 
     .......... 
userfield.rb 에 정의 된 0

pdf이 에 defined이 아니기 때문에 오류가 발생했다고 가정합니다!

undefined local variable or method `pdf' for Formular:Class 

이제 제 질문은 어디에서 그런 방법을 사용해야합니까? 그래서 내가 각 변수를 전달하지 않아도됩니까?

감사

당신은 또한 클래스 변수라는 PDF 파일을 추가 할 수 있습니다

답변

0

:

그것은 클래스의 모든 방법을 공유 할 것입니다 그리고 당신은 @pdf PDF로 전화 할 수
attr_accessor :pdf 

.

예 :

> module Animal 
> def say 
     @word 
> end 
> end 
> class Doge 
> include Animal 
> attr_accessor :word 
> def initialize 
>  @word = 'such cool feature' 
> end 
> end 

> dog = Doge.new 
> dog.say 
=> 'such cool feature' 
관련 문제