小编Fir*_*cer的帖子

是否可以在Ruby中使用名为`class`或其他保留名称的关键字参数?

许多Rails标记助手(例如content_tag等)使用名为的hash选项参数class来指定HTML类.

content_tag(:div, content_tag(:p, "Hello world!"), class: "strong")
Run Code Online (Sandbox Code Playgroud)

对于新代码,我想使用关键字参数,但是当其中一个是语言关键字时,它是否可能?

例如,对于假设link_tag.

def link_tag(url, class: nil)
  html = ''
  html << '<a href="' << url << '"'
  html << ' class="' << class << '"' if class
  ...


def link_tag(url, opts={})
  html = ''
  html << '<a href="' << url << '"'
  html << ' class="' << opts[:class] << '"' if opts[:class]
  ...
Run Code Online (Sandbox Code Playgroud)

ruby

2
推荐指数
1
解决办法
82
查看次数

Java让PrintWriter写两个不同的Writer

我正在重构一些代码,并希望制作一个PrintWriter,将输出发送到两个单独的Writer(而不是流,它们最终会转到不同的地方,其中一个可能有其他东西从其他地方发送到它).

对于流有Apache TeeOutputStream,是否有什么适用于Writer或我需要通过流和返回文本?

java io

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

C++ DLL:不暴露整个类

如何"隐藏"类的某些部分,以便使用该库的人不必包含我班级中使用的所有类型的标题.即下面的MainWindow类,我可以拥有它,所以当在静态/动态库中编译时,无论谁使用libary都不必包含windows.h,即HWND,CRITICAL_SECTION,LRESULT等不必定义.

我知道我可以把它分成两个类,一个只有公共接口的抽象类,一个隐藏的实现类,它包含需要windows.h的成员.

这里的问题是无法再创建可见类,并且需要额外的创建函数(例如CreateMainWindow).在这种情况下,这很好,因为最有可能只需要在堆上创建的单个实例,但对于其他类,则不是这样.

class MainWindow
{
    HWND hwnd;
    int width, height;
    std::string caption;
    bool started,exited;
    bool closeRequest;

    unsigned loopThread;
    CRITICAL_SECTION inputLock;

    Input *input;
public:
    static void init_type();
    Py::Object getattr(const char *name);

    MainWindow(int width, int height, std::string caption);
    ~MainWindow();

    bool CloseRequest(const Py::Tuple &args);
    bool CloseRequestReset(const Py::Tuple &args);

    HWND GetHwnd();

    int GetWidth();
    int GetHeight();

    Input* GetInput();
protected:
    unsigned static __stdcall loopThreadWrap(void *arg);
    unsigned LoopThreadMain();

    LRESULT WndProc(UINT msg, WPARAM wParam, LPARAM lParam);
    LRESULT static CALLBACK WndProcWrapper(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
617
查看次数

类模板特化问题

我正在尝试将一些代码从VC9移植到G ++,但是我遇到了模板特化的问题,显然不允许类成员使用.

以下代码是类方法的getValue特化的这些错误的示例.在所有情况下,错误都是"错误:非命名空间范围中的显式特化class ..."

template<typename T> T getValue(const_iterator key)const
{
    try{return boost::lexical_cast<T>(key->second);}
    catch(boost::bad_lexical_cast &e)
    {
        throw TypeParseError<T>(name, key->first, e.what());
    }
}
template<typename T> T getValue(const std::string &key)const
{
    iterator i = find(key);
    if(i == end())throw KeyNotFound(name,key);
    else return getValue(i);
}
template<> std::string getValue<std::string>(const_iterator key)const
{
    return key->second;
}
template<> std::string getValue<std::string>(const std::string &key)const
{
    const_iterator i = find(key);
    if(i == end())throw KeyNotFound(name,key);
    else return i->second;
}
Run Code Online (Sandbox Code Playgroud)

它是否只是不支持确切的语法,并且一个小的更改将使它工作,或者我是否需要更改代码以避免像这样的专业化?如果后者这是一般的最佳方式吗?

c++ scope g++ specialization visual-c++

0
推荐指数
1
解决办法
242
查看次数

ActionMailer SMTP“证书验证失败”

我想从我的Rails Web应用程序发送电子邮件,并且我不想禁用TLS证书验证。但是,由于某种原因,即使服务器证书有效,它也始终会失败并显示“ SSLv3读取服务器证书B:证书验证失败”。

我再次检查了openssl s_client(使用/etc/ssl/certs/ca-certificates.crt),并且在rails控制台中运行以下命令也可以成功交付。

smtp = Net::SMTP.new(host, port)
smtp.enable_tls
smtp.start("localhost", username, password, :login) do |smtp|
  smtp.send_message msgstr, from, to
end
Run Code Online (Sandbox Code Playgroud)

该服务器具有Rails 4.2.6和Ruby 2.3.0

config.action_mailer.smtp_setting = {
    address: 
    port: 465,
    user_name: 
    password: 
    authentication: :login,
    openssl_verify_mode: OpenSSL::SSL::VERIFY_PEER,
    enable_starttls_auto: false,
    ssl: true
}
Run Code Online (Sandbox Code Playgroud)

ruby ssl smtp ruby-on-rails

0
推荐指数
1
解决办法
1792
查看次数

标签 统计

c++ ×2

ruby ×2

g++ ×1

io ×1

java ×1

ruby-on-rails ×1

scope ×1

smtp ×1

specialization ×1

ssl ×1

visual-c++ ×1