小编Kri*_*ris的帖子

使用'pull'或'merge'合并本地分支?

我已经看到了两种不同的方法来合并本地分支.

git checkout master
git merge new_feature


git checkout master
git pull . new_feature
Run Code Online (Sandbox Code Playgroud)

有什么区别,利弊?

git merge branch pull

9
推荐指数
1
解决办法
2334
查看次数

如何使用nokogiri和rubyzip编辑docx

我正在使用rubyzip和nokogiri的组合来编辑.docx文件.我正在使用rubyzip来解压缩.docx文件,然后使用nokogiri来解析和更改word/document.xml文件的正文,但是我最后关闭rubyzip它会破坏文件而我无法打开它或者修理它.我在桌面上解压缩.docx文件并检查word/document.xml文件,并将内容更新为我更改为的内容,但所有其他文件都搞砸了.有人可以帮我解决这个问题吗?这是我的代码:

require 'rubygems'  
require 'zip/zip'  
require 'nokogiri'  
zip = Zip::ZipFile.open("test.docx")  
doc = zip.find_entry("word/document.xml")  
xml = Nokogiri::XML.parse(doc.get_input_stream)  
wt = xml.root.xpath("//w:t", {"w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}).first  
wt.content = "New Text"  
zip.get_output_stream("word/document.xml") {|f| f << xml.to_s}  
zip.close
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails docx rubyzip nokogiri

8
推荐指数
2
解决办法
4600
查看次数

在Rails中config.action_mailer.smtp_settings和ActionMailer :: Base.smtp_settings有什么区别?

我在RoR应用程序中设置了Exchange兼容的邮件服务器.我使用了以下设置development.rb:

config.action_mailer.smtp_settings = {
  :address              => 'mail.server.com',
  :port                 => 5870,
  :user_name            => 'username',
  :password             => 'password',
  :authentication       => :login
}
Run Code Online (Sandbox Code Playgroud)

我得到这个设置不起作用 Net::SMTPAuthenticationError: 504 Unrecognized authentication type.

但是,如果我应用完全相同的配置environment.rb,它可以完美地工作:

ActionMailer::Base.smtp_settings = {
  :address              => 'mail.server.com',
  :port                 => 5870,
  :user_name            => 'username',
  :password             => 'password',
  :authentication       => :login  
}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?不应该config.action_mailer.smtp_settings设置相同的设置?这是一个错误吗?它有原因吗?

我按照这里的说法尝试了它,它有效,所以smtp_settings确实对邮件有影响,但在我看来并非所有的选项都计算/工作.

ruby-on-rails actionmailer

8
推荐指数
1
解决办法
2585
查看次数

如何为JSON解析指定数据类型?

我有一个JSON响应,它是一个哈希数组:

[{"project" => {"id" => 1, "name" => "Internal"},
 {"project" => {"id" => 2, "name" => "External"}}]
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样:

client = HTTP::Client.new(url, ssl: true)
response = client.get("/projects", ssl: true)
projects = JSON.parse(response.body) as Array
Run Code Online (Sandbox Code Playgroud)

这给了我一个数组,但似乎我需要对元素进行类型转换以实际使用它们,否则我得到undefined method '[]' for Nil (compile-time type is (Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type))).

我试过as Array(Hash)但是这给了我can't use Hash(K, V) as generic type argument yet, use a more specific type.

如何指定类型?

crystal-lang

7
推荐指数
1
解决办法
2445
查看次数

如何在子类中的方法之前和之后运行代码?

我的第一个想法是这样的事情:

class AbstractBuilder
  attr_reader :time_taken

  def build_with_timer
    started_at = Time.now
    build
    @time_taken = Time.now - started_at
  end

  def build
    raise 'Implement this method in a subclass' 
  end
end

class MyBuilder < AbstractBuilder
  def build
    sleep(5)
  end
end

builder = MyBuilder.new.build_with_timer
puts builder.time_taken
Run Code Online (Sandbox Code Playgroud)

我怀疑有一种更好的方法可以提供更好的灵活性,例如理想情况下我想在MyBuilder的实例上调用'build'而不是'build_with_timer'并且总是记录执行时间.

我确实考虑过使用初始化的alias_method,甚至使用模块mixin而不是类继承,它会覆盖在中间调用super的构建方法(不确定是否可行).在我走下兔子洞之前,我想我会看看是否有既定的做法.

ruby metaprogramming

6
推荐指数
1
解决办法
4401
查看次数

Ruby 中的 Avro 序列化 - 如何写入字符串/缓冲区而不是文件

我正在尝试在 Ruby 中进行 avro 序列化。我已经编写了 JSON 模式,但是我喜欢将序列化数据作为 Ruby 中的字节而不是写入文件。

我的代码挂了类似的东西:

SCHEMA = {
           "type": "record",
           "name": "User",
           "fields" :
             [
               {"name": "name", "type": "string"},
               {"name": "id", "type": "long"},
               {"name": "city", "type": "string"}
             ]
         }.to_json

schema = Avro::Schema.parse(SCHEMA)
dw = Avro::IO::DatumWriter.new(schema)
buffer = StringIO.new
encoder = Avro::IO::BinaryEncoder.new(buffer)
???
Run Code Online (Sandbox Code Playgroud)

我有 name、id 和 city 的值,想知道如何创建 User 对象并将其序列化到字符串/字节缓冲区中。

ruby serialization avro

6
推荐指数
1
解决办法
2606
查看次数

如何拉扯相对线?

我打开了相对的行号.

我可以用10号线猛拉:10y.

但是我如何猛拉当前线下面的第5行而没有跳到所说的线,yanking和跳回(即5jY5k).

如果我有这个文件:

2   describe 'foobar' do
1     it 'should be cool' do
46      # do stuff
1     end
2  end
Run Code Online (Sandbox Code Playgroud)

我在第46行,我想要在相对的第1或第2行,无论是上方还是下方.

vim

6
推荐指数
1
解决办法
484
查看次数

使用模块使用"has_many"扩展插件中的模型

我在引擎样式插件中有一些代码,其中包含一些模型.在我的应用程序中,我想扩展其中一个模型.我已经设法通过在初始化程序中包含一个模块,将实例和类方法添加到相关模型中.

但是,我似乎无法添加关联,回调等.我得到'找不到方法'错误.

/libs/qwerty/core.rb

module Qwerty
  module Core
    module Extensions

      module User
        # Instance Methods Go Here 

        # Class Methods
        module ClassMethods
          has_many  :hits, :uniq => true # no method found

          before_validation_on_create :generate_code # no method found

          def something # works!
            "something"
          end
        end

        def self.included(base)
          base.extend(ClassMethods)
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

/initializers/qwerty.rb

require 'qwerty/core/user'

User.send :include, Qwerty::Core::Extensions::User
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

5
推荐指数
3
解决办法
5167
查看次数

如何为未向主机应用程序公开的 Rails 引擎创建 rake 任务?

# lib/tasks/test.rake
task :hello do
  puts 'hello'
end
Run Code Online (Sandbox Code Playgroud)

$ rake 应用程序:你好

要运行任务,我需要在它前面加上“app:”前缀,它在虚拟应用程序的上下文中运行。它也作为rake hello.

我想运行一个不需要 Rails 环境并运行一些命令的 rake 任务,但它是从引擎根目录运行的,而不是虚拟应用程序根目录。

rake ruby-on-rails-plugins ruby-on-rails-3 ruby-on-rails-3.1

5
推荐指数
1
解决办法
5236
查看次数

用ActiveRecord检查是否存在多条记录?

当至少有一个键存在时,使用#exists?会产生误报(即返回 true)。

MyModel.exist?(key: [:risk_score, :i_made_this_up]) # => true
Run Code Online (Sandbox Code Playgroud)

对于少量检查,我可以这样做:

[:risk_score, :i_made_this_up].all? { |key| MyModel.exists?(key: key) }
Run Code Online (Sandbox Code Playgroud)

但这是一个N+1查询,如果我有很多键要检查怎么办?

ruby-on-rails ruby-on-rails-5

5
推荐指数
1
解决办法
1762
查看次数