我最近开始使用Ruby编码,并且对某些行为感到困惑.
我正在使用2.2.3p173并显示以下内容:
__ENCODING__ #=> #<Encoding:UTF-8> Default encoding in 2.2.3
"my_string".encoding #=> #<Encoding:UTF-8>
Object.to_s.encoding #=> #<Encoding:US-ASCII>
Object.new.to_s.encoding #=> #<Encoding:ASCII-8BIT>
Run Code Online (Sandbox Code Playgroud)
这种编码差异的原因是什么?
我已经搜索了相关的帖子并尝试过几个方面,但是在使用Python3语法时我仍然在Vim中收到语法错误.
收到错误的代码:
types_of_people = 10; f"There are {types_of_people} types of people."
Run Code Online (Sandbox Code Playgroud)
运行输出$ vim --version:
$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 17 2017 10:39:50)
MacOS X (unix) version
Included patches: 1-1100
Compiled by Homebrew
Huge version without GUI. Features included (+) or not (-):
+acl +file_in_path +mouse_sgr +tag_old_static
+arabic +find_in_path -mouse_sysmouse -tag_any_white
+autocmd +float +mouse_urxvt -tcl
-balloon_eval +folding +mouse_xterm +termguicolors
-browse -footer +multi_byte +terminal
++builtin_terms +fork() +multi_lang +terminfo
+byte_offset -gettext -mzscheme +termresponse …Run Code Online (Sandbox Code Playgroud) 我喜欢在Ruby中如何使用Symbol#to_proc以下方法将方法作为块传递:
[1.0, 2.0, 3.0].map(&:to_i)
#=> [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我也可以定义自己的lambda,times_two并将其作为块传递:
times_two = ->(x) {x * 2}
[1, 2, 3].map(×_two)
#=> [2, 4, 6]
Run Code Online (Sandbox Code Playgroud)
虽然我似乎不能times_two作为象征传递:
[1, 2, 3].map(&:times_two)
#=> ArgumentError: wrong number of arguments (0 for 1)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用方法执行相同操作时,我收到错误:
def times_three(x)
x * 3
end
[1, 2, 3].map(×_three)
#=> ArgumentError: wrong number of arguments (0 for 1)
[1, 2, 3].map(&:times_three)
#=> ArgumentError: wrong number of arguments (0 for 1)
Run Code Online (Sandbox Code Playgroud)
我猜我不能这样做因为times_three是一种方法,而不是Proc.
那么如何定义自定义方法以便Symbol#to_proc像to_i …
我试图找出如何加载我的更改,.bash_profile而不必注销我的Bash会话或退出终端,也不会影响我的$PATH.
在新的会话中,这是我的$PATH:
/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin
Run Code Online (Sandbox Code Playgroud)
这是我设置重新加载我的别名.bash_profile:
alias reload='source ~/.bash_profile'
Run Code Online (Sandbox Code Playgroud)
当我运行reload命令时,我的所有更改.bash_profile都被加载,但$PATH每次运行命令时都会延长.
例如:
>> reload
>> echo $PATH
/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin
Run Code Online (Sandbox Code Playgroud)
你可以看到我$PATH现在更长了,我认为这是因为以下是我的.bash_profile:
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/heroku/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)
如何防止$PATH每次重新加载时更新?
我尝试通过将建议的行添加到我的Rails项目的Gemfile来安装rubocop:
gem 'rubocop', require: false
运行后bundle,它会安装并在我的Gemfile.lock中可见.
但是,当$ rubocop从我的应用程序的根运行时,我收到此错误:
-bash: rubocop: command not found
我试过跑步$ which rubocop,什么都没发生.
我也试过跑步$ gem install rubocop,行为没有区别.
以下是我的Gemfile.lock的摘录:
rubocop (0.27.0)
astrolabe (~> 1.3)
parser (>= 2.2.0.pre.6, < 3.0)
powerpack (~> 0.0.6)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
Run Code Online (Sandbox Code Playgroud) 当我尝试配置 Git 时,收到以下错误:
$ git config --global user.name "John Smith"
error: could not lock config file /Users/John/.gitconfig: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我的根目录完全缺少一个.gitconfig文件。当我尝试制作新的时,我收到此错误:
$ touch ~/.gitconfig
touch: /Users/John/.gitconfig: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我的 git 位置:
$ which git
/usr/local/git/bin/git
Run Code Online (Sandbox Code Playgroud)
我已经尝试重新安装 Git 几次(运行 OSX Yosemite),但仍然遇到同样的问题。正如有关此问题的一些帖子中提到的,我也找不到任何 .gitconfig.lock 文件。