为什么这个Ruby对象to_s和inspect看起来做同样事情的方法?
该p方法调用inspect并放置/打印to_s用于表示对象的调用.
如果我跑
class Graph
def initialize
@nodeArray = Array.new
@wireArray = Array.new
end
def to_s # called with print / puts
"Graph : #{@nodeArray.size}"
end
def inspect # called with p
"G"
end
end
if __FILE__ == $0
gr = Graph.new
p gr
print gr
puts gr
end
Run Code Online (Sandbox Code Playgroud)
我明白了
G
Graph : 0
Graph : 0
Run Code Online (Sandbox Code Playgroud)
to_s和之间有什么区别inspect?puts,print以及p?如果我注释掉 …
我最近在fedora 12中安装了rails.我也是linux新手.在Windows 7上一切正常.但我在linux中面临很多问题.请帮忙!
我已经安装了所有必需品,以使基本脚本/服务器启动并运行.我在尝试脚本/服务器时弹出boot.rb时出现此错误.我想在此提供的一些细节:
安装rails,ruby和gem的目录,
[vineeth@localhost my_app]$ which ruby
/usr/local/bin/ruby
[vineeth@localhost my_app]$ which rails
/usr/bin/rails
[vineeth@localhost my_app]$ which gem
/usr/bin/gem
Run Code Online (Sandbox Code Playgroud)
当我运行脚本/服务器时,这就是错误.
[vineeth@localhost my_app]$ script/server
./script/../config/boot.rb:9:in `require': no such file to load -- rubygems (LoadError)
from ./script/../config/boot.rb:9
from script/server:2:in `require'
from script/server:2
Run Code Online (Sandbox Code Playgroud)
PATH文件看起来像这样
[vineeth@localhost my_app]$ cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin/ruby:$PATH"
Run Code Online (Sandbox Code Playgroud)
我想这与PATH文件有关.让我知道我需要在这里改变什么.如果我还有其他变化,请告诉我.
在Java中,你可以做到instanceof.是否有Ruby等价物?
根据我对self关键字的理解,它只是引用了类的当前实例.这不是一直都是默认行为吗?例如,不是
self.var_one = method(args)相当于var_one = method(args)?
如果是这样那么自我的用途是什么?
在Java中我可能会这样做:
public static void doSomething();
Run Code Online (Sandbox Code Playgroud)
然后我可以静态访问该方法而无需创建实例:
className.doSomething();
Run Code Online (Sandbox Code Playgroud)
我怎么能在Ruby中做到这一点?这是我的课程,根据我的理解self.,该方法是静态的:
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我试着打电话时:
Ask.make_permalink("make a slug out of this line")
Run Code Online (Sandbox Code Playgroud)
我明白了:
undefined method `make_permalink' for Ask:Class
Run Code Online (Sandbox Code Playgroud)
如果我没有声明该方法是私有的,为什么呢?
我注释掉了一个gem,但'bundle install'仍然无法运行.如何找出哪个gem依赖于sys-proctable?
$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Could not find sys-proctable-0.9.2 in any of the sources
$ grep proctable Gemfile
#gem 'sys-proctable', '0.9.2', :path => "vendor/gems"
$ bundle list
Resolving dependencies...
Could not find gem 'rspec-rails (= 2.11.0) ruby' in the gems available on this machine.
$ bundle viz
Resolving dependencies...
Could not find gem 'rspec-rails (= 2.11.0) ruby' in the gems available on this machine.
$ bundle -v
Bundler …Run Code Online (Sandbox Code Playgroud) 我的项目是从第三方gem扩展开源类,我们不希望保持与我们自己的代码相同的编码标准.重构gem代码不是一个可行的选择.我们只是想让Rubocop忽略复制的代码.
如何指示Rubocop完全忽略文件或目录?
http://betterspecs.org/#subject有一些关于subject和的信息let.但是,我仍然不清楚它们之间的区别.此外,SO帖子反对在RSpec测试中使用之前,之后和主题的论点是什么?说最好不要使用subject或者let.我该去哪儿?我感到很困惑.
我真正问的问题是为什么要求不采用宝石的名称.而且,如果没有,那么最简单的方法就是找到秘密咒语来要求该死的东西!?
作为一个例子,如果我已经memcache-client安装,那么我必须要求它使用
require 'rubygems'
require 'memcache'
Run Code Online (Sandbox Code Playgroud)