Sometimes we face the erorr “Gem::FilePermissionError: You don’t have write permissions for the /Library/Ruby/Gems/x.x.x directory”. This error message indicates that the user doesn’t have the necessary permissions to install Ruby gems system-wide. This is because the system Ruby installation requires administrative privileges to modify its gem directory.
To resolve this issue, there is a few options:
1. Use a Ruby Version Manager (Recommended)
Instead of installing gems globally, consider using a Ruby version manager like rbenv
or RVM
. These tools allow you to manage multiple Ruby versions and gems without requiring administrative permissions.
Using rbenv
1. Install rbenv if you haven't already.
2. Install the desired version of Ruby using rbenv.
3. Install gems without needing elevated permissions.
Using RVM
1. Install RVM if you haven't already.
2. Install the desired version of Ruby using RVM.
3. Use the installed Ruby version without needing elevated permissions to install gems.
IO
Ruby程序在启动后会预先分配3个IO对象:
标准输入 - 预定义常量(STDIN), 全局变量($stdin)
标准输出 - 预定义常量(STDOUT), 全局变量($stdout)
标准错误输出 - 预定义常量(STDERR), 全局变量($stderr)
1 | $stdout.print "output something from $stdout\n" |
IO对象是否与控制台关联,我们可以通过**tty?**方法来判断。
1 | if $stdin.tty? |
字符串比较
Ruby中比较字符串是否相同也是用**==和!=**等运算符
1 | #whether two strings are equal |
如果是判断两字符串是否相似,采用正则表达式匹配更加简单。
1 | #whether two strings are similar |
字符串的创建
普通创建
Ruby中使用**” “或‘ ‘符号来创建字符串,在字符串中也可以包含表达式#{}**,这个表达式被称之为内嵌表达式。如果字符串中需要包含””或者’’等字符时,则可以通过转义字符\来实现。
1 | str = "this is a string" |
Octopress was broken after I upgraded Mac to OS X EI Capitan 10.11.1 in these days. The details of the problem is below.
Octopress is work well in Mac10.10. but when I upgraded to Mac10.11.1(OS X EI Capitan). And put rake preview in terminal to see the blog what looks like, got following error.
Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
rake aborted!
Errno::ENOENT: No such file or directory - compass
/Users/user/git/octopress/Rakefile:85:in spawn
/Users/user/git/octopress/Rakefile:85:in block in <top (required)>
Tasks: TOP => preview
(See full trace by running task with --trace)
替换数组内的元素
根据指定规则替换数组中的元素,Ruby也提供了以下方法:
ary.collect { |item| …}
ary.collect! { |item| …}
ary.map { |item| …}
ary.map! { |item| …}
将数组ary中的各元素item传递给块做处理。
1 | ary = [1, 2, 3] |