小编Dav*_*man的帖子

Ruby语法问题:Rational(a,b)和Rational.new!(a,b)

今天我在Rational类中遇到了奇怪的ruby语法:

Rational(a,b)
Run Code Online (Sandbox Code Playgroud)

(注意缺少.new()与普通Ruby语法相比的部分).与正常new语法相比,这意味着什么呢?更重要的是,我如何在我自己的代码中实现这样的东西,为什么我会实现这样的东西?特别是对于Rational类,为什么使用这种语法而不是正常的实例化?为什么这个new方法在理性类中是私有的?(以及我如何/为什么要在我自己的ruby代码中执行此操作?)提前感谢您的答案,特别是因为我问了很多问题.

ruby syntax constructor instantiation parentheses

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

使用YARD进行Ruby文档的内部注释?

我正在将从RDoc维护的rubygem切换到YARD文档.但是,代码中有一些关键注释只需要保留在代码中,不应该出现在文档中.例如:

##
# SomeClass documentation here.
#--
# CRITICAL comment that should be in the code but not in the documentation,
#          and must be at this particular spot in the code.
#++
# more documentation that follows the critical comment block, but this part 
# should be in the generated documentation
class SomeClass
    ...
end
Run Code Online (Sandbox Code Playgroud)

RDoc尊重#--#++门,但YARD没有.什么(如果存在)是在YARD标记中做类似事情的语法?

ruby rdoc yard

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

防止将构造函数参数隐式转换为外部库类型

请考虑以下代码:

#include <boost/range.hpp>
#include <boost/iterator/counting_iterator.hpp>

typedef boost::iterator_range<boost::counting_iterator<int>> int_range;

template <typename T>
class Ref {
    T* p_;    
  public:    
    Ref(T* p) : p_(p) { }
    /* possibly other implicit conversion constructors,
       but no unconstrained template constructors that don't
       use the explicit keyword... */  
    operator T*() const { return p_; }
    operator const T*() const { return p_; }    
};

struct Bar { };

class Foo {    
  public:    
    Foo(int a, char b) { /* ... */ }    
    Foo(int a, const Ref<Bar>& b) { …
Run Code Online (Sandbox Code Playgroud)

c++ constructor boost implicit-conversion c++11

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

部分专业化消歧优先链的更好模式?

考虑以下系列的部分专业化:

template <typename T, typename Enable=void>
struct foo {
  void operator()() const { cout << "unspecialized" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_integral<T>::value
>>{
  void operator()() const { cout << "is_integral" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  sizeof(T) == 4
    and not is_integral<T>::value
>>{
  void operator()() const { cout << "size 4" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_fundamental<T>::value
    and not (sizeof(T) == 4)
    and not is_integral<T>::value
>>{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates idioms partial-specialization

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

推导嵌套别名模板的模板参数时的编译器差异

考虑下面的代码片段,我们试图在该代码片段中推断出函数的template参数foobar()

struct Bar { static constexpr auto size = 5; };

template <class T, class=std::make_index_sequence<T::size>>
struct FooList;
template <class T, std::size_t... Idxs>
struct FooList<T, std::integer_sequence<std::size_t, Idxs...>> { };

struct Wrapper {
  template <class T>
  using list_type = FooList<T>;
};

template <class T>
void foobar(typename Wrapper::template list_type<T>) { }

void test() { 
  foobar(FooList<Bar>{});
}
Run Code Online (Sandbox Code Playgroud)

主要的编译器前端在是否应进行编译方面存在分歧(请参阅this godbolt)。如果我们显式地指定template参数,例如:

void test_explicit() { 
  foobar<Bar>(FooList<Bar>{});
}
Run Code Online (Sandbox Code Playgroud)

所有主要的前端都能成功地编译代码(godbolt)。有趣的是,如果我们简化推论:

struct Bar { };

struct Wrapper {
  template <class T>
  using my_type …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer template-argument-deduction

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

输入目录时在zsh中输入文件

有没有办法在输入特定目录时获取特定文件来设置环境?有点像rvm,但更一般.

environment zsh

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

一些Perl的细微之处

我已经在Perl中编程了一段时间,但我从未理解过Perl的一些细微之处:

$ _变量的使用和设置/取消设置让我感到困惑.例如,为什么呢

# ...
shift @queue;
($item1, @rest) = split /,/;
Run Code Online (Sandbox Code Playgroud)

工作,但(至少对我而言)

# ...
shift @queue;
/some_pattern.*/ or die();
Run Code Online (Sandbox Code Playgroud)

似乎不起作用?

另外,我不明白使用foreach与文件迭代文件之间的区别while.例如,我似乎得到了不同的结果

while(<SOME_FILE>){  
    # Do something involving $_        
}
Run Code Online (Sandbox Code Playgroud)

foreach (<SOME_FILE>){
    # Do something involving $_
}
Run Code Online (Sandbox Code Playgroud)

谁能解释这些微妙的差异?

perl

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

rdoc、darkfish 和 :call-seq: 标签

我正在使用 rdoc 记录 Ruby 项目,并且发现了 darkfish rdoc 格式化程序。我真的很喜欢它,但:call-seq:标签不再起作用了。相反,它将文字字符串放入:call-seq:文档中,然后将调用序列本身格式化为代码块。我不想只从代码中取出所有 :call-seq: 块,因为我的大部分文档需要引用块中给出的实例名称和参数名称:call-seq:。有没有其他人有这个问题?我应该做什么,有解决方法吗?我很确定:call-seq:标签在我使用默认格式化程序之前可以工作,但我不能确定,因为我不知道如何返回生成原始格式(调用 rdoc 时不带任何参数,除了文件生成现在,即使我删除了 doc 文件夹,darkfish 也会输出!)有谁知道如何解决这个问题?

ruby documentation rdoc

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

在Objective-C中进行子类化

我对Objective-C有点新意,我一直在尝试做一些显然不被允许的事情,尽管这是其他语言的常见做法(我认为).

作为一个具体的例子,我想要NSMutableArray创建一个SortedMutableArray始终保持自己处于排序状态的子类.所以我NSMutableArray以通常的方式子类化,添加一个NSComparator确定排序顺序的属性.我重写了addObject:以排序方式插入对象的方法:

- (void) addObject:(id)anObject {
    for (int i = 0; i < [self count]; ++i) {
        NSComparisonResult result = (NSComparisonResult)self.comparator([self objectAtIndex:i], anObject); 
        if (result == NSOrderedDescending || result == NSOrderedSame) {
            [super insertObject:anObject atIndex:i];
            break;
        }
        else {
            if (result != NSOrderedAscending) {
                [NSException raise:@"InvalidBlockException" format:@"Block must return one of NSOrderedDescending, NSOrderedAscending, or NSOrderedSame"];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好.但是当我运行程序时,我收到一个错误,表明它insertObject:atIndex:现在是抽象的,需要实现.阅读文档,它列出了必须在任何子类中实现的几种方法,NSMutableArray其中一种方法确实如此insertObject:atIndex:.但我不需要改变功能insertObject:atIndex:; 我想让它完全像它一样NSMutableArray …

inheritance objective-c

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