如何为exercism.io的Perl 5仓库做贡献
本周早些时候,我在exercism.io文章中呼吁大家将练习题移植到Perl 5仓库。今天,我将一步一步地介绍移植过程,并展示如何轻松地做出贡献。
要求
您需要一个GitHub账户并已安装Perl。仅此而已!
分叉仓库
要向GitHub上的项目做出贡献,我们将使用“分叉和拉取”方法。首先,我们需要登录到GitHub
接下来,搜索exercism/xperl5仓库
点击“分叉”按钮,将仓库复制到我们自己的perltricks/xperl5仓库
获取练习题
现在我们已经分叉了仓库,我们可以对我们的分叉版本进行提交更改。首先,我们需要从我们的分叉仓库下载Perl练习题。我们可以从命令行完成此操作
$ git clone https://github.com/sillymoose/xperl5.git
这将把xperl5仓库下载到名为“xperl5”的目录中。接下来,下载常用练习题列表
$ git clone https://github.com/exercism/x-common.git
这将下载最新可用的练习题列表到“x-common”目录,其中包含一系列练习题的readme文件。任何在x-common目录中有readme文件而在xperl5目录中缺失的练习题都需要移植。
在另一种语言中找到练习题
一旦找到需要移植的练习题,您将想要在另一种语言的仓库中找到该练习题。翻译练习题比从头开始编写它容易得多!Ruby、Python和JavaScript仓库包含大多数练习题,所以我们将从其中之一开始。例如,要下载Ruby练习题仓库,只需输入以下命令
$ git clone https://github.com/exercism/xruby.git
如果xruby目录中没有您想要移植的练习题,请尝试克隆xpython或xjavascript。
移植练习题
要移植练习题,您需要提供练习题测试文件和一个通过测试的Example.pm模块。本周早些时候,我将Ruby到Perl的“leap”练习题进行了移植。这涉及三个步骤。首先,我在xperl5目录中创建了新的练习题子目录
$ mkdir xperl5/leap
接下来,我将Ruby测试文件“xruby/leap/leap_test.rb”
require 'date'
require 'minitest/autorun'
require_relative 'year'
class Date
def leap?
throw "Try to implement this yourself instead of using Ruby's implementation."
end
alias :gregorian_leap? :leap?
alias :julian_leap? :leap?
end
class YearTest < MiniTest::Unit::TestCase
def test_leap_year
assert Year.leap?(1996)
end
def test_non_leap_year
skip
refute Year.leap?(1997)
end
def test_non_leap_even_year
skip
refute Year.leap?(1998)
end
def test_century
skip
refute Year.leap?(1900)
end
def test_fourth_century
skip
assert Year.leap?(2400)
end
end
翻译成“xperl5/leap/leap.t”
use warnings;
use strict;
use Test::More tests => 7;
my $module = $ENV{EXERCISM} ? 'Example' : 'Leap';
my $sub = $module . '::is_leap';
use_ok($module) or BAIL_OUT ("You need to create a module called $module.pm.");
can_ok($module, 'is_leap') or BAIL_OUT("Missing package $module with sub is_leap().");
do {
no strict 'refs';
is 1, $sub->(1996), '1996 is a leap year';
is 0, $sub->(1997), '1997 is not a leap year';
is 0, $sub->(1998), '1998 is not a leap year';
is 0, $sub->(1900), '1900 is not a leap year';
is 1, $sub->(2400), '2400 is a leap year';
}
最后,我将示例答案“xruby/leap/example.rb”移植过来
require 'delegate'
class Year < SimpleDelegator
def self.leap?(number)
Year.new(number).leap?
end
def leap?
divisible_by?(400) || divisible_by?(4) && !divisible_by?(100)
end
private
def divisible_by?(i)
(self % i) == 0
end
end
以下是Perl版本,“xperl5/leap/Example.pm”
package Example;
use warnings;
use strict;
sub is_leap {
my $year = shift;
divisible_by($year, 400)
or divisible_by($year, 4) and !divisible_by($year, 100)
? 1 : 0;
}
sub divisible_by {
$_[0] % $_[1] == 0 ? 1 : 0;
}
__PACKAGE__;
在命令行中运行测试文件
$ EXERCISM=1 prove leap.t
leap.t .. ok
All tests successful.
Files=1, Tests=7, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.02 cusr 0.00 csys = 0.04 CPU)
Result: PASS
所有测试都通过了,因此我们可以提交这些文件。我还移植了Perl 6版本。
将新练习题添加到分叉仓库
现在我们已经移植了文件,我们需要将它们添加到分叉的xperl5仓库并提交更改。以下是完成此操作的命令
$ cd xperl5
$ git add leap/Example.pm leap/leap.t
$ git commit -am 'Added the leap exercise'
$ git push origin master
如果分叉仓库与exercism/xperl5不同步,您需要重新基。
创建拉取请求
返回到GitHub,我们只需从我们的分叉仓库perltricks/xperl5发起拉取请求。点击屏幕右侧的“拉取请求”链接将我们带到拉取请求屏幕
点击“新拉取请求”按钮将创建拉取请求表单,GitHub自动知道拉取请求应返回到exercism/xperl5。
点击“发送拉取请求”按钮提交拉取请求,我们完成了!exercism仓库的提交者通常在几小时内就会回复。所以现在您已经看到了如何轻松地移植练习题,警告:这可能会上瘾……
喜欢这篇文章吗?帮助我们,推文关于它!
本文最初发布在 PerlTricks.com。
标签
反馈
这篇文章有什么问题吗?请通过在 GitHub 上打开一个问题或拉取请求来帮助我们。