简体   繁体   English

在 ruby​​ 中调用 do 块内的方法

[英]Calling a method inside a do block in ruby

I'm trying to include files from all git submodules in a gemspec file.我正在尝试将所有 git 子模块中的文件包含在 gemspec 文件中。 The gemspec reads as following gemspec 如下

require File.expand_path(File.dirname(__FILE__)) + '/lib/nameofgem.rb'
# Gets all files, from all submodules, and returns them
# as an array
def getSubmoduleFiles()
  files = []

  # get an array of submodule dirs by executing 'pwd' inside each submodule
  `git submodule --quiet foreach pwd`.split("\n").each do |submodule_path|

    gemRootDir = File.dirname(File.expand_path(__FILE__))

    # for each submodule, change working directory to that submodule
    Dir.chdir(submodule_path) do
      submodule_files = getSubmoduleFiles()
      # issue git ls-files in submodule's directory
      submodule_files + `git ls-files`.split("\n")

      puts "found:"
      puts submodule_files.to_s
      puts

      # prepend the submodule path to create absolute file paths
      submodule_files_fullpaths = submodule_files.map do |filename|
        "#{submodule_path}/#{filename}"
      end

      # remove leading path parts to get paths relative to the gem's root dir
      # (this assumes, that the gemspec resides in the gem's root dir)
      submodule_files_paths = submodule_files_fullpaths.map do |filename|
        filename.gsub "#{gemRootDir}/", ""
      end

      # add relative paths to gem.files
      files += submodule_files_paths
    end
  end

  return files
end

Gem::Specification.new do |s|
  s.name        = 'name'
  s.version     = "1.1.1"
  s.executables << 'exec'
  s.licenses    = ['LICENCE']
  s.summary     = "Does stuff"
  s.description = "Longer description of stuff"
  s.authors     = ["author"]
  s.email       = 'my@email'
  s.files       = `git ls-files -- lib/*`.split("\n")
  s.homepage    = 'https://example.com/'
  s.required_ruby_version = '>= 2.0.0'

  s.files += getSubmoduleFiles()
end

But in the do block, I get但是在 do 块中,我得到

Invalid gemspec in [nameofgem.gemspec]: undefined method `getSubmoduleFiles' for Gem::Specification:::Module

What am I doing wrong?我究竟做错了什么? Why can't a call the function from within the do block?为什么不能从 do 块中调用该函数?

It seems that gemspec files do some weird stuff with scoping and namespacing (in the entire file, not just within the gemspec block itself), but after banging my head against my desk for 15 minutes and downing yet another beer to dull the existential torment induced by my inability to call a function in a gemspec, I figured out a reasonably clean solution: just stick self.似乎 gemspec 文件在作用域和命名空间方面做了一些奇怪的事情(在整个文件中,而不仅仅是在 gemspec 块本身内),但是在我的头撞在我的桌子上 15 分钟后,又喝了一口啤酒以减轻对存在的折磨由于我无法在 gemspec 中调用函数,我想出了一个相当干净的解决方案:只是坚持self. in front of the function name in both the definition and invocation, like so:在定义和调用中的函数名前面,像这样:

def self.foo
  puts 'It works :)'
end

Gem::Specification.new do |s|
  # ...
  self.foo  # should print 'It works :)' instead of erroring out
  # ...
end

Why this is necessary, and why nobody else seems to have asked or answered this question anywhere on the World Wide Web, I haven't the faintest idea, but here we go.为什么这是必要的,为什么在万维网上的任何地方似乎都没有其他人问过或回答过这个问题,我没有最模糊的想法,但我们走了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM