Ruby'dagi modul usullarida uni qayta ishlatish uchun moduldagi ma'lumotlarni o'rnatishning bir yo'li bormi?

Menda quyidagicha ta'riflangan ma'ruza sinf mavjud:

class Report

include Parser

 def initialize(file: file)
  @file = remove_bookmarks(file: parse(file)) 
  @proofread_text = original(file: @file)
  @original_text = proofread(file: @file)
  @paragraphs = paragraphs(file: @file)
end


end

Parser moduli quyidagicha:

module Parser

    def parse(file:file)
      zip_file = Zip::File.open(file) 
    end

    def remove_bookmarks(file: file)
      file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end

    def proofread(file: file)
       text(@file, filter: 'del')
    end

    def original(file: file)
      text(@file)
    end

    def paragraphs(file: file)
     file.xpath('//w:p[w:ins or w:del]')
    end 

    def text(file: file, filter: 'ins')
      xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
    end

end

Mening boshlang'ich usulim bir xil faylni Parser modulida turli uslublarga o'tayotganini payqadim.

Modeldagi fayl ma'lumotlarini modul ichida ishlatish uchun faylning ob'ektini har safar o'tkazishga majbur qilishning imkoni yo'qmi?

0

9 javoblar

Faqat misol o'zgaruvchilarini ishlatishingiz kerak. Buni hatto bir necha joyda o'tkazgansiz.

class Report

  include Parser

   def initialize(file: file)
     parse_file
     remove_bookmarks
     @proofread_text = original
     @original_text = proofread
     @paragraphs = paragraphs
   end

end

module Parser

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      @file.at("//w:bookmarkStart").remove if @file.at("//w:bookmarkStart")
      @file.at("//w:bookmarkEnd").remove if @file.at("//w:bookmarkEnd")
    end

    ...

end

Siz kabi bir narsa qilishingiz mumkin:

module Parser

    attr_reader :file

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end

    ...

end

If you are using a recent version of ruby you can probably also change file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart") to the simpler file.at("//w:bookmarkStart")&.remove

1
qo'shib qo'ydi
File.at ("// w: bookmarkStart") uchun Marcga rahmat. ruby versiya 2.2.3 dan foydalanmoqdaman. Kodni sinab ko'rganimda, bu menga "kutilmagan" xatolik berdi. ''. ruby 2.4 versiyasiga kerakmi?
qo'shib qo'ydi muallif chell, manba
Tashakkur Marc. Men funktsiyani qaytarish qiymatini, ajoyib g'oyani saqlab qolaman va yangi sintaksisdan foydalanish uchun ruby 2.3 ga uriladi.
qo'shib qo'ydi muallif chell, manba
Ruby 2.3 da tanishtirildi, uni xavfsiz navigatsiya operatori deb atashdi. Ishlash muammo bo'lsa, ni funktsiyasida (sekin ko'rinadigan) qaytariladigan qiymatini saqlashni hisobga olishni istayman.
qo'shib qo'ydi muallif Marc Rohloff, manba

Faqat misol o'zgaruvchilarini ishlatishingiz kerak. Buni hatto bir necha joyda o'tkazgansiz.

class Report

  include Parser

   def initialize(file: file)
     parse_file
     remove_bookmarks
     @proofread_text = original
     @original_text = proofread
     @paragraphs = paragraphs
   end

end

module Parser

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      @file.at("//w:bookmarkStart").remove if @file.at("//w:bookmarkStart")
      @file.at("//w:bookmarkEnd").remove if @file.at("//w:bookmarkEnd")
    end

    ...

end

Siz kabi bir narsa qilishingiz mumkin:

module Parser

    attr_reader :file

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end

    ...

end

If you are using a recent version of ruby you can probably also change file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart") to the simpler file.at("//w:bookmarkStart")&.remove

1
qo'shib qo'ydi
File.at ("// w: bookmarkStart") uchun Marcga rahmat. ruby versiya 2.2.3 dan foydalanmoqdaman. Kodni sinab ko'rganimda, bu menga "kutilmagan" xatolik berdi. ''. ruby 2.4 versiyasiga kerakmi?
qo'shib qo'ydi muallif chell, manba
Tashakkur Marc. Men funktsiyani qaytarish qiymatini, ajoyib g'oyani saqlab qolaman va yangi sintaksisdan foydalanish uchun ruby 2.3 ga uriladi.
qo'shib qo'ydi muallif chell, manba
Ruby 2.3 da tanishtirildi, uni xavfsiz navigatsiya operatori deb atashdi. Ishlash muammo bo'lsa, ni funktsiyasida (sekin ko'rinadigan) qaytariladigan qiymatini saqlashni hisobga olishni istayman.
qo'shib qo'ydi muallif Marc Rohloff, manba

Faqat misol o'zgaruvchilarini ishlatishingiz kerak. Buni hatto bir necha joyda o'tkazgansiz.

class Report

  include Parser

   def initialize(file: file)
     parse_file
     remove_bookmarks
     @proofread_text = original
     @original_text = proofread
     @paragraphs = paragraphs
   end

end

module Parser

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      @file.at("//w:bookmarkStart").remove if @file.at("//w:bookmarkStart")
      @file.at("//w:bookmarkEnd").remove if @file.at("//w:bookmarkEnd")
    end

    ...

end

Siz kabi bir narsa qilishingiz mumkin:

module Parser

    attr_reader :file

    def parse(file:file)
      @file = Zip::File.open(file) 
    end

    def remove_bookmarks
      file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end

    ...

end

If you are using a recent version of ruby you can probably also change file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart") to the simpler file.at("//w:bookmarkStart")&.remove

1
qo'shib qo'ydi
File.at ("// w: bookmarkStart") uchun Marcga rahmat. ruby versiya 2.2.3 dan foydalanmoqdaman. Kodni sinab ko'rganimda, bu menga "kutilmagan" xatolik berdi. ''. ruby 2.4 versiyasiga kerakmi?
qo'shib qo'ydi muallif chell, manba
Tashakkur Marc. Men funktsiyani qaytarish qiymatini, ajoyib g'oyani saqlab qolaman va yangi sintaksisdan foydalanish uchun ruby 2.3 ga uriladi.
qo'shib qo'ydi muallif chell, manba
Ruby 2.3 da tanishtirildi, uni xavfsiz navigatsiya operatori deb atashdi. Ishlash muammo bo'lsa, ni funktsiyasida (sekin ko'rinadigan) qaytariladigan qiymatini saqlashni hisobga olishni istayman.
qo'shib qo'ydi muallif Marc Rohloff, manba

Ha, siz parser moduli usulida @file nusxa ko'chiruvchi parametrini sinfdagi modulni o'z ichiga olsangiz, modul usullari sinfning namuna usullari kabi harakat qilishingiz mumkin.

Quyida ko'rsatilganidek @file dan foydalanishingiz mumkin.

class Report

include Parser

  def initialize(file: file)
    @file = remove_bookmarks(file: parse(file)) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def parse(file:file)
    zip_file = Zip::File.open(file) 
  end

  def remove_bookmarks(file: file)
    file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
    file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
  end

  def proofread
    text(@file, filter: 'del')
  end

  def original
    text(@file)
  end

  def paragraphs
    @file.xpath('//w:p[w:ins or w:del]')
  end 

  def text(file: file, filter: 'ins')
    xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end
end

Agar ehtiyot bo'lish kerak bo'lgan narsa bo'lsa, parser moduli har qanday boshqa sinfga kiritilgan bo'lsa, siz ayrıştırıcı moduli biron bir usuli foydalanishdan oldin @file o'zgaruvchilarini yaratgandirsiz.

1
qo'shib qo'ydi

Ha, siz parser moduli usulida @file nusxa ko'chiruvchi parametrini sinfdagi modulni o'z ichiga olsangiz, modul usullari sinfning namuna usullari kabi harakat qilishingiz mumkin.

Quyida ko'rsatilganidek @file dan foydalanishingiz mumkin.

class Report

include Parser

  def initialize(file: file)
    @file = remove_bookmarks(file: parse(file)) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def parse(file:file)
    zip_file = Zip::File.open(file) 
  end

  def remove_bookmarks(file: file)
    file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
    file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
  end

  def proofread
    text(@file, filter: 'del')
  end

  def original
    text(@file)
  end

  def paragraphs
    @file.xpath('//w:p[w:ins or w:del]')
  end 

  def text(file: file, filter: 'ins')
    xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end
end

Agar ehtiyot bo'lish kerak bo'lgan narsa bo'lsa, parser moduli har qanday boshqa sinfga kiritilgan bo'lsa, siz ayrıştırıcı moduli biron bir usuli foydalanishdan oldin @file o'zgaruvchilarini yaratgandirsiz.

1
qo'shib qo'ydi

Ha, siz parser moduli usulida @file nusxa ko'chiruvchi parametrini sinfdagi modulni o'z ichiga olsangiz, modul usullari sinfning namuna usullari kabi harakat qilishingiz mumkin.

Quyida ko'rsatilganidek @file dan foydalanishingiz mumkin.

class Report

include Parser

  def initialize(file: file)
    @file = remove_bookmarks(file: parse(file)) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def parse(file:file)
    zip_file = Zip::File.open(file) 
  end

  def remove_bookmarks(file: file)
    file.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
    file.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
  end

  def proofread
    text(@file, filter: 'del')
  end

  def original
    text(@file)
  end

  def paragraphs
    @file.xpath('//w:p[w:ins or w:del]')
  end 

  def text(file: file, filter: 'ins')
    xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end
end

Agar ehtiyot bo'lish kerak bo'lgan narsa bo'lsa, parser moduli har qanday boshqa sinfga kiritilgan bo'lsa, siz ayrıştırıcı moduli biron bir usuli foydalanishdan oldin @file o'zgaruvchilarini yaratgandirsiz.

1
qo'shib qo'ydi

Report sinfida attr_reader: file ni aniqlasangiz, siz sintaksik tahlil qiluvchi modulidan faylni chaqira olasiz. Biroq, dalillarni nomlash bilan ehtiyot bo'ling. zip_file dan foydalanishni tavsiya qilaman.

class Report
  include Parser

  attr_reader :file

  def initialize(zip_file)
    @file = remove_bookmarks(zip_file) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def remove_bookmarks(zip_file)
    parse(zip_file)
    zip_file.tap do |z|
      z.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      z.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end
  end

  def proofread
    text('del')
  end

  def original
    text
  end

  def paragraphs
    file.xpath('//w:p[w:ins or w:del]')
  end 

  private

  def text(filter = 'ins')
    file.xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end

  def parse(zip_file)
    Zip::File.open(zip_file) 
  end

end
1
qo'shib qo'ydi

Report sinfida attr_reader: file ni aniqlasangiz, siz sintaksik tahlil qiluvchi modulidan faylni chaqira olasiz. Biroq, dalillarni nomlash bilan ehtiyot bo'ling. zip_file dan foydalanishni tavsiya qilaman.

class Report
  include Parser

  attr_reader :file

  def initialize(zip_file)
    @file = remove_bookmarks(zip_file) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def remove_bookmarks(zip_file)
    parse(zip_file)
    zip_file.tap do |z|
      z.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      z.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end
  end

  def proofread
    text('del')
  end

  def original
    text
  end

  def paragraphs
    file.xpath('//w:p[w:ins or w:del]')
  end 

  private

  def text(filter = 'ins')
    file.xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end

  def parse(zip_file)
    Zip::File.open(zip_file) 
  end

end
1
qo'shib qo'ydi

Report sinfida attr_reader: file ni aniqlasangiz, siz sintaksik tahlil qiluvchi modulidan faylni chaqira olasiz. Biroq, dalillarni nomlash bilan ehtiyot bo'ling. zip_file dan foydalanishni tavsiya qilaman.

class Report
  include Parser

  attr_reader :file

  def initialize(zip_file)
    @file = remove_bookmarks(zip_file) 
    @proofread_text = original
    @original_text = proofread
    @paragraphs = paragraphs
  end
end

module Parser
  def remove_bookmarks(zip_file)
    parse(zip_file)
    zip_file.tap do |z|
      z.at("//w:bookmarkStart").remove if file.at("//w:bookmarkStart")
      z.at("//w:bookmarkEnd").remove if file.at("//w:bookmarkEnd")
    end
  end

  def proofread
    text('del')
  end

  def original
    text
  end

  def paragraphs
    file.xpath('//w:p[w:ins or w:del]')
  end 

  private

  def text(filter = 'ins')
    file.xpath("//text()[not(ancestor::w:#{filter})]").map(&:text).join("")
  end

  def parse(zip_file)
    Zip::File.open(zip_file) 
  end

end
1
qo'shib qo'ydi