给定一个数组:
arr = [['a', '1'], ['b','2'], ['c', '3']]
Run Code Online (Sandbox Code Playgroud)
什么是将它分成两个数组的最佳方法?
例如,从上面的数组我想得到以下两个数组:
first = ['a','b','c']
second = ['1', '2', '3']
Run Code Online (Sandbox Code Playgroud)
我能用这个collect吗?
我停用了用户注册(Gem Devise),并且想进行测试以确保路由/users/sign_up不存在。
为此,我在 spec/features/user_spec.rb
require 'spec_helper'
require 'capybara/rspec'
feature "Users" do
scenario "could not register " do
expect(:get => "/users/sign_up").not_to be_routable
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,出现以下错误:
1) Users could not register
Failure/Error: expect(:get => "/users/sign_up").not_to be_routable
NoMethodError:
undefined method `routable?' for {:get=>"/users/sign_up"}:Hash
# ./spec/features/user_spec.rb:8:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud) 说我有以下课程:
class Person
def initialize(name, age)
@name = name
@age = age
end
def get_age
return @age
end
end
Run Code Online (Sandbox Code Playgroud)
我有一系列的Person对象.是否有一种简洁的,类似Ruby的方式来获得最小(或最大)年龄的人?怎么样按它排序呢?
我有一个数组:
a = [ 0,9,6,12,1]
Run Code Online (Sandbox Code Playgroud)
我需要一种方法按降序排序:
a = [12,9,6,1,0]
Run Code Online (Sandbox Code Playgroud)
为了按升序排序我有一个Ruby函数a[].to_a.sort,我正在寻找一个函数来按降序对数组进行排序.
想象一下包括类似这样的类:
class Element
include Comparable
attr_accessor :name, :pos_x, :pos_y
def initialize(name, pos_x, pos_y)
@name = name
@pos_x = pos_x
@pos_y = pos_y
end
def <=>(other)
if (@pos_x == other.pos_x) and (@pos_y == other.pos_y)
return 0
else
return @name <=> other.name
end
end
def eql?(other)
self == other
end
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下hash,你将如何实现这个功能a.hash == b.hash?一般来说,我会这样做:
def hash
@name.hash
end
Run Code Online (Sandbox Code Playgroud)
但这不包括pos_x和pos_y.
我有一个Dictionary包含键的值.根据我的条件,我将有重复的密钥,严格禁止使用Dictionary.现在我的问题是:如何检查当前的重复键Dictionary并删除它以添加新的?
我正在Rails应用程序中编写jsonify视图.我有:
json.samba_secrets_present(not @idp_ad_info.samba_secrets.nil?)
Run Code Online (Sandbox Code Playgroud)
这会产生语法错误:
app/views/idp_ad_infos/show.jsonify:7: syntax error, unexpected tIVAR, expecting '('
Run Code Online (Sandbox Code Playgroud)
然而,
json.samba_secrets_present (not @idp_ad_info.samba_secrets.nil?)
Run Code Online (Sandbox Code Playgroud)
工作良好.我原以为第一个方法samba_secrets_present是Jsonify::Builder使用第一个参数调用对象上的方法not idp_ad_info.samba_secrets.nil?.为什么空间有意义?
我创建了一个新的Rails应用程序.当我尝试启动服务器时,出现以下错误:
[bathakarai@Project1-CO samp]$ rails server
/home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.13/lib/rails/script_rails_loader.rb:11: warning: Insecure world writable dir /usr in PATH, mode 040777
/home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/bundler-1.3.4/lib/bundler/runtime.rb:216: warning: Insecure world writable dir /usr in PATH, mode 040777
/home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/execjs-1.4.0/lib/execjs.rb:4:in `<top (required)>'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `<top (required)>'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
from /home/bathakarai/.rvm/gems/ruby-2.0.0-p0/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) …Run Code Online (Sandbox Code Playgroud) 在.end一个Ruby范围的是用于指定范围的结束,所述范围是否是"专用的"末尾的数字.我不明白这个设计决定的理由,但从来没有这么简单,我想知道什么是最惯用的机制来确定范围的"有效结束"(即最大的整数n,range.include?(n)就是这样true.我所知道的唯一机制)是的last(1)[0],这看起来很笨拙.
我想知道 Ruby 中是否有与 JavaScript 函数等价的fromCharCode函数。它的作用是将 Unicode 值转换为字符。
下面是 JavaScript 中返回值的示例:
String.fromCharCode(72,69,76,76,79)
#=> HELLO
Run Code Online (Sandbox Code Playgroud)
Ruby 中有类似的东西吗?