有没有办法在ruby中使实例变量"私有"(C++或Java定义)?换句话说,我希望以下代码导致错误.
class Base
def initialize()
@x = 10
end
end
class Derived < Base
def x
@x = 20
end
end
d = Derived.new
Run Code Online (Sandbox Code Playgroud) 当我尝试在 case 语句中匹配字符串 '3' 时,它匹配范围是否达到 '9',而不是 '10'。
我猜它与三重等号运算符有关,但我不知道它可以在范围内但不匹配的确切原因。
这是一个 IRB 运行,记录了有效(使用“9”)和不起作用(使用“10”)的两种情况:
case '3'
when ('0'...'9')
puts "number is valid"
else
puts "number is not valid"
end
Run Code Online (Sandbox Code Playgroud)
输出: number is valid
case '3'
when ('0'...'9')
puts "number is valid"
else
puts "number is not valid"
end
Run Code Online (Sandbox Code Playgroud)
输出: number is not valid
我用作预期结果参考的方法是
Enumerable#include?
Enumerable#member?
,查看转换为数组时输出的内容是 ( Enumerable#to_a)。
“大小写相等” ( ===) 运算符的结果让我感到惊讶。
case '3'
when ('0'...'10')
puts "number is valid"
else
puts "number is not valid"
end
Run Code Online (Sandbox Code Playgroud) 像许多其他人一样,我总是认为"对于Ruby来说,纯粹的编译器永远不会存在,因为这种语言对于静态编译器来说太过动态了."
但我最近偶然发现了这些:
这两个项目似乎都非常有趣.它们可以为我们提供本机编译语言的速度(以及编译语言的商业需求,混淆代码),同时保留Ruby的所有(或大部分)优雅和灵活性.添加一个好的支持库(或者更可能是访问现有C++库的可能性),你可以很容易地理解为什么这些东西会很有趣.
有人试过Crystal语言吗?(我还没有,因为ruby-llvm的编译问题)
这是他/她对此的感受?
您是否认为,鉴于这些设计选择,实际上是否可以为Ruby开发本机代码(机器代码)编译器(在合理的时间内合理努力)?它会有意义吗?
我试图将一长串文本截断到一定长度,但也希望确保截断的结果在空格处结束.之后我还要附加一个省略号.
例如:
"This is a very long string that has more characters than I want in it."
Run Code Online (Sandbox Code Playgroud)
成为这个:
"This is a very long string that..."
Run Code Online (Sandbox Code Playgroud)
我从这开始,但显然这不涉及在空格上结束字符串的问题.
<%= item.description[0..30] %>…
Run Code Online (Sandbox Code Playgroud) Date.today在Ruby中调用返回当前日期.但是,它的时区是什么?我假设UTC,但我想确定.该文件没有说明任何一种方式.
我正在使用Ruby on Rails,我正在尝试使用可选参数创建一个方法.显然,有很多方法可以做到这一点.我尝试将可选参数命名为哈希值,而不定义它们.输出是不同的.看一看:
# This functions works fine!
def my_info(name, options = {})
age = options[:age] || 27
weight = options[:weight] || 160
city = options[:city] || "New York"
puts "My name is #{name}, my age is #{age}, my weight is #{weight} and I live in {city}"
end
my_info "Bill"
-> My name is Bill, my age is 27, my weight is 160 and I live in New York
-> OK!
my_info "Bill", age: 28
-> My name is Bill, …Run Code Online (Sandbox Code Playgroud) 我想知道在OS X 10.8 Mountain Lion上为Ruby 1.9.3构建开发机器的替代方法,它不需要Xcode.
Mountain Lion现在是Golden Master,因为我正在写这个问题,所以它可以被认为是最终版本.关于Xcode,这是一个预览版本.
RVM建议在Xcode上安装osx-gcc-installer,但我不想弄乱我的系统.
在没有Xcode的情况下在Mountain Lion上安装Ruby 1.9.3的最简洁方法是什么?
我有一个图片模型,其中包含视图计数(整数)的变量.每当有人查看Picture对象时,视图计数就会增加+1.
完成这项工作后,有什么区别
@picture.view_count += 1
@picture.save
Run Code Online (Sandbox Code Playgroud)
和
@picture.increment(:view_count, 1)
Run Code Online (Sandbox Code Playgroud)
如果我使用增量,还是.save必要吗?
为什么"啜饮"文件不是普通文本文件I/O的好习惯,何时有用?
例如,为什么我不应该使用这些?
File.read('/path/to/text.txt').lines.each do |line|
# do something with a line
end
Run Code Online (Sandbox Code Playgroud)
要么
File.readlines('/path/to/text.txt').each do |line|
# do something with a line
end
Run Code Online (Sandbox Code Playgroud) 我在捆绑我的Gemfile时遇到问题.我已经安装了Nokogiri但是当我运行bundle install它时无法加载Nokogiri.
安装Nokogiri:
gem install nokogiri
Building native extensions. This could take a while...
Successfully installed nokogiri-1.6.6.2
Parsing documentation for nokogiri-1.6.6.2
Done installing documentation for nokogiri after 2 seconds
1 gem installed
Run Code Online (Sandbox Code Playgroud)
捆绑安装:
bundle install sic@ANTHONYs-iMac
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
Using minitest 5.5.1
Using thread_safe 0.3.4
Using tzinfo 1.2.2
Using activesupport 4.2.0
Using builder 3.2.2
Using …Run Code Online (Sandbox Code Playgroud)