小编Dav*_*ght的帖子

Ruby 中如何通过引用传递?

目前,我正在 Ruby 中开发一个Watcher类,除其他外,它能够找到切换信号的周期持续时间。这通常相当简单,但我面临的一个问题是 Ruby 显然按值传递所有参数。在网上研究时,我发现了许多关于“按值传递”和“按引用传递”实际上是什么的不同讨论,但没有实际的“如何”。对于具有 C/C++ 背景的我来说,这是编程/脚本语言的重要组成部分。

我编写的课程的相关部分如下所示。Watcher::watchToggling() 方法是我遇到问题的方法。作为参数,aVariable应该是对我正在测量周期持续时间的值的引用。

class Watcher
  @done
  @timePeriod

  def reset()
    @done = false;
    @timePeriod = nil;
  end

  def watchToggling(aVariable, aTimeout=nil)
    oldState = aVariable;
    oldTime = 0;
    newTime = 0;

    timeout(aTimeout)do
      while(!@done)
        newState = aVariable;
        if(newState != oldState)
          if(newState == 1)
            # rising edge
            if(oldTime != 0)
              # there was already a rising edge before,
              # so we can calculate the period
              newTime = Time.now();
              @timePeriod = newTime - oldTime;
              @done = …
Run Code Online (Sandbox Code Playgroud)

ruby parameter-passing pass-by-reference pass-by-value pass-by-pointer

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

强制 Git 用户在工作副本中设置电子邮件地址

显然,可以使用以下gitconfig设置强制 Git 用户为每个新存储库添加电子邮件地址:

git config --global user.email "(none)"
git config --global user.useConfigOnly true
Run Code Online (Sandbox Code Playgroud)

我所做的。我的gitconfig -l输出(截断):

core.symlinks=true
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
diff.astextplain.textconv=astextplain
rebase.autosquash=true
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.required=true
filter.lfs.process=git-lfs filter-process
credential.helper=manager
core.autocrlf=true
core.eol=native
user.name=David Wright
user.email=(none)
user.useconfigonly=true
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用。我可以在不设置电子邮件地址的情况下提交。提交中列出的电子邮件地址(哦,奇迹)是“(无)”。

我正在使用git version 2.13.0.windows.1.

我想这样做是因为我在同一台计算机上使用两个不同的帐户。我能够实现在我的ssh-config文件中应用不同 ssh 密钥的类似设置:

# github account
Host github.com
Port 22
HostName github.com
User git
IdentityFile <file>
Run Code Online (Sandbox Code Playgroud)

但显然 git 本身没有这种可能性。

无论如何,我希望能够强制我必须为每个存储库设置一个用户电子邮件地址,并且我希望它成为我全局gitconfig …

git git-config

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