我在学习 Ruby 的同时解决了一些关于 codewars 的问题。我解决了这个一个更C ++ -杂交的方式(我还是新来的Ruby)
然后我来检查基于upvotes的最佳解决方案,即:
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.chars : iterable).chunk { |x| x }.map(&:first)
end
Run Code Online (Sandbox Code Playgroud)
我不知道那(&:first)是什么。我知道地图的作用,并且在我运行时看起来:
[1, 2, 3, 4, 4, 5].chunk {|x| x}.map(&:first)
Run Code Online (Sandbox Code Playgroud)
删除重复的元素。
在Tcl中,如果您有一个包含偶数元素的列表,则可以一次循环两个元素.看到这段代码:
foreach { a b } [ list 1 2 3 4 ] {
puts "${a} ${b}"
}
Run Code Online (Sandbox Code Playgroud)
将输出:
1 2
3 4
Run Code Online (Sandbox Code Playgroud)
如何用Ruby获得相同的行为?
我有时使用#tryfrom ActiveSupport:
1.try(:not_a_method)
#=> nil
Run Code Online (Sandbox Code Playgroud)
耶!没有抛出异常,但假设我不想要 nil:
1.better_than_try(:not_a_method){0}
#=> 0
Run Code Online (Sandbox Code Playgroud)
这存在吗?
我正在 codewars 中进行这个练习,即展平数组(最多一层深),例如,我想获得输出:
[1,2,3] >> [1,2,3]
[[1,2],3] >> [1,2,3]
[[1,[2]],3] >> [1,[2],3]
Run Code Online (Sandbox Code Playgroud)
我决定使用“注入” - 如果该元素是数组,则下一个元素将添加到运行总数中,如果不是,则直接推送:
def flatten(array)
array.inject([]) {|result,element| element.kind_of?(Array) result.concat(element) : result<<element}
end
Run Code Online (Sandbox Code Playgroud)
谁能帮助解释为什么我收到以下语法错误?
-e:3: syntax error, unexpected tIDENTIFIER, expecting '}'
... element.kind_of?(Array) result.concat(element) : result<
Run Code Online (Sandbox Code Playgroud) 我输入了这样的东西:
input = [['abc',['xyz','1.1']], ['abc',['xyz','1.2']],['def',['lmn','3.14']]]
Run Code Online (Sandbox Code Playgroud)
我想把它转换成
{'abc'=>[{'xyz'=>'1.1'},{'xyz'=>'1.2'}],'def'=>[{'lmn'=>'3.14'}]}
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我在控制台上使用:
User.all(:order => "created_at ASC")
Run Code Online (Sandbox Code Playgroud)
通过从我的模型用户用Rubyonrails.org替换客户端,但它给了我这个错误:
ArgumentError: wrong number of arguments (1 for 0)
Run Code Online (Sandbox Code Playgroud)
它出什么问题了?
我正在开发一个rails应用程序,我需要比较如下:
url == "http://localhost:3000/admin/admin_login"
Run Code Online (Sandbox Code Playgroud)
我想知道有没有办法比较像:
url == "*/admin/admin_login"
Run Code Online (Sandbox Code Playgroud) 我有两个哈希数组,想要从中做一个
first =
[{:frontman=>"aaa", :category=>"bbb", :subcategory=>nil, :detail=>nil},other hashes]
second =
[{:__content__=>"aaa", :id=>"9096290", :frontman=>"aaa"},other hashes]
Run Code Online (Sandbox Code Playgroud)
我希望有
一大堆哈希
[{:__content__=>"aaa", :id=>"9096290", :frontman=>"aaa", :category=>"bbb", :subcategory=>nil, :detail=>nil},other hashes]
Run Code Online (Sandbox Code Playgroud)
我试过了
(first+second).group_by{|h| h[:frontman]}.map{|k,v| v.reduce(:merge)}
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用
我仍然能够快速掌握我的Ruby技能.
我一直试图弄清楚我是否可以为rspec测试干掉这段代码:
require 'rails_helper'
RSpec.describe Role, type: :model do
describe "has several roles types " do
before :each do
@roles = Role.all
end
context "The role types that are valid" do
it "includes 'proofreader', 'admin', 'super_admin' " do
expect(@roles.any?{|r| r.name == 'proofreader'}).to be_truthy
expect(@roles.any?{|r| r.name == 'admin'}).to be_truthy
expect(@roles.any?{|r| r.name == 'super_admin'}).to be_truthy
end
end
context "The role types that are not valid " do
it "includes 'developer' " do
expect(@roles.any?{|r| r.name == 'developer'}).to be_falsy
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
具体来说,我想将这三行代码减少到一行,但是我不知道如何在一行中检查三个字符串值的ActiveRecord关系.有任何想法吗?
我想在反引号中传递JSON格式的字符串作为脚本参数,例如:
stats = "[[\"25/01/2017 03:15 PST\",
\"26/01/2017 03:15 PST\",
\"27/01/2017 03:15 PST\",
\"28/01/2017 03:15 PST\"]]"
`my_executable_script.js "#{stats}"`
Run Code Online (Sandbox Code Playgroud)
并获得stdout值(这很重要).问题是JSON参数应该使用双引号进行转义,例如:
my_executable_script.js "[[\"25/01/2017 03:15 PST....."
Run Code Online (Sandbox Code Playgroud)
而是my_executable_script.js [[\"25/01/2017 03:15 PST.....]]执行(没有""),这是错误的.我尝试了不同的逃避方式,没有成功.
我不能使用system()方法,因为我需要执行脚本的结果.