ruby 代码帮助

发布于 2024-12-03 16:56:15 字数 1323 浏览 3 评论 0原文

我是 Ruby 初学者,无法理解这段代码

    require_relative 'custom_page'

module Jekyll   
  class Tag < CustomPage
    def initialize(site, base, dir, tag)
      super site, base, dir, 'tag'

      self.data['tag'] = tag
      self.data['title'] = "#{site.config['tag_title_prefix'] || 'Tag: '}#{tag}"
      self.data['description'] = "#{site.config['tag_meta_description_prefix'] || 'Tag: '}#{tag}"
    end
  end

  class Tags < CustomPage
    def initialize(site, base, dir)
      super site, base, dir, 'tags'
      self.data['tags'] = site.categories.keys.sort
      #1# puts self.data['tags']
    end
  end

  class Site
    # generate_tags_categories is called by the custom process function in site_process.rb

    def generate_tags_categories            
      dir = self.config['tag_dir'] || 'tags'
      write_page Tags.new(self, self.source, dir) if self.layouts.key? 'tags'        

      self.categories.keys.each do |tag|
      puts "dd"
      #2# puts tag    
      write_page Tag.new(self, self.source, File.join(dir, tag.slugize), tag)
      end
    end
  end
end

在上面的代码中,语句 puts self.data['tags'] (标记为 1)按预期输出超过 10 个值。但是,puts tag 行(标记为 2)仅输出一个值,这意味着该数组仅包含一个值。 self.categories.keys.each 是否期望循环遍历本身分配给 self.data['tags'] 的所有值

I am beginner in Ruby and having trouble understanding this code

    require_relative 'custom_page'

module Jekyll   
  class Tag < CustomPage
    def initialize(site, base, dir, tag)
      super site, base, dir, 'tag'

      self.data['tag'] = tag
      self.data['title'] = "#{site.config['tag_title_prefix'] || 'Tag: '}#{tag}"
      self.data['description'] = "#{site.config['tag_meta_description_prefix'] || 'Tag: '}#{tag}"
    end
  end

  class Tags < CustomPage
    def initialize(site, base, dir)
      super site, base, dir, 'tags'
      self.data['tags'] = site.categories.keys.sort
      #1# puts self.data['tags']
    end
  end

  class Site
    # generate_tags_categories is called by the custom process function in site_process.rb

    def generate_tags_categories            
      dir = self.config['tag_dir'] || 'tags'
      write_page Tags.new(self, self.source, dir) if self.layouts.key? 'tags'        

      self.categories.keys.each do |tag|
      puts "dd"
      #2# puts tag    
      write_page Tag.new(self, self.source, File.join(dir, tag.slugize), tag)
      end
    end
  end
end

In the above code, the statement puts self.data['tags'] (marked 1) outputs more than 10 values as expected. However, the line puts tag (marked 2) outputs only one value implying that array contains only one value. Isn't self.categories.keys.each expected to loop through all the values which itself is assigned to self.data['tags']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-12-10 16:56:15

在进入循环之前,您可以首先确保“类别”仍然包含许多值:

puts "categories.keys: #{self.categories.keys.inspect}" # <<- here
self.categories.keys.each do |tag|
  puts "dd"
  #2# puts tag

如果第一个 puts 显示多个值,并且仅调用循环一次,那么您可能需要调查可能导致循环中断的原因。也许 write_page 抛出异常,捕获调用堆栈的某个位置?

当您调试某些值时,最好使用 inspect 方法而不是(由 puts 自动调用)方法 to_sinspect 的输出更加易于调试。

“标签”有可能(尽管不太可能)包含一些控制字符,这些控制字符会清除以前的输出或类似的内容。 inspect 始终是安全的。 (好吧,并非总是如此,但在大多数情况下;)

You may start with ensuring that the 'categories' still contain many values before entering the loop:

puts "categories.keys: #{self.categories.keys.inspect}" # <<- here
self.categories.keys.each do |tag|
  puts "dd"
  #2# puts tag

If the first puts shows you more than one value, and the loop is called only once, then you may want to investigate what possibly makes the loop break. Maybe write_page throws an exception, catch somewhere up the calling stack?

When you debug some values, it's better to use inspect method instead of (automagically called by puts) method to_s. The output of inspect is more debug-friendly.

It is possible, (however unlikely) that the 'tag' contains some control characters which clear the previous output, or something like that. The inspect is always safe. (ok, not always but in most of cases ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文