我有一个"名称"数组,我想显示当前用户的名称,只要它是数组中唯一的名称.如果数组中还有其他名称,我不想显示当前用户的名称.
目前,我列出了名称并排除了当前用户的名称,但如果它是数组中唯一的名称,则不希望排除当前用户的名称.我希望我解释说没关系.
我的代码现在:
module ConversationsHelper
def other_user_names(conversation)
users = []
conversation.messages.map(&:receipts).each do |receipts|
users << receipts.select do |receipt|
receipt.receiver != current_user
end.map(&:receiver)
end
users.flatten.uniq.map(&:name).join ', '
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个方法,包括(在后续行中)以下代码:
candidate_lines.each do |line|
return line if priority_1?(line) && line.marked_by?(self)
end
Run Code Online (Sandbox Code Playgroud)
据我所知,其中的方法是无关紧要的.是什么让我感到困惑,我有一个测试应该只是通过我给出的规范跳过该方法的一部分:
it "without any priority 1 lines, returns all priority 2 lines marked by itself" do
line1 = double :line, marked_by?: true
line2 = double :line, marked_by?: true
allow(joshua).to receive(:priority_1?).and_return(:false)
expect(joshua).to receive(:candidate_lines).and_return([line1, line2])
expect(joshua.tiebreak_lines).to eq [line1, line2]
end
Run Code Online (Sandbox Code Playgroud)
然而,当我运行测试时,返回语句将被触发.我已经在'return line ...'行之前使用puts语句手动测试了它,结果是,'priority_1?(line)'返回false,由于某种原因'priority_1?(line)&& line. marked_by?(self)'正在评估为true.
这里发生了什么事?
我正在学习Ruby中符号的使用,并且已经意识到它们主要用作对变量,哈希表中的键的引用,甚至作为在方法中发送块的方式.
我的问题是,什么是符号,例如:+ :- :*,当我在方法中使用它们时引用?
例如,:+用于汇总数组中的所有值:
puts [1,2,3].reduce(:+)
=> 6
Run Code Online (Sandbox Code Playgroud)
给出与以下相同的结果:
puts [1,2,3].reduce {|sum, i| sum += i}
=> 6
Run Code Online (Sandbox Code Playgroud)
如果我创建自己的版本:+
a = lambda {|sum,i| sum += i}
puts [1,2,3].reduce(&a)
=> 6
Run Code Online (Sandbox Code Playgroud)
因此,我的第一个想法是:+引用{|sum, i| sum += i}作为一个明确的块,但我很难找到确认的信息.
我正在调试一个问题simple_token_authentication,我修改了https://github.com/gonzalo-bulnes/simple_token_authentication/blob/master/lib/simple_token_authentication/sign_in_handler.rb#L7中的一些代码:
def sign_in(controller, record, *args)
begin
puts "=== TRACE 1"
integrate_with_devise_trackable!(controller)
puts "=== TRACE 2"
controller.send(:sign_in, record, *args)
puts "=== TRACE 3"
rescue Exception => e
puts "=== TRACE 4"
ensure
puts "=== TRACE 5"
end
end
Run Code Online (Sandbox Code Playgroud)
输出:
Started GET "/projects" for ::1 at 2016-04-18 18:35:22 +0800
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProjectsController#index as JSON
Parameters: {"project"=>{}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? ORDER BY "users"."id" ASC …Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby 2.3.如何在字符串的开头添加一定数量的空格?我认为rjust是这样做的,但是当我想在字符串的前面添加1个填充空间时,这些调用什么都不做.
line = " 29 25 13 M10-19 14 23:36 7:36 826 HYLLBERG MARCO WI Kenosha"
# => " 29 25 13 M10-19 14 23:36 7:36 826 HYLLBERG MARCO WI Kenosha"
line.rjust(1)
# => " 29 25 13 M10-19 14 23:36 7:36 826 HYLLBERG MARCO WI Kenosha"
Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来保护/隐藏/加密我的JavaScript代码.通过一些研究,我得到你不能那样做.我发现只有你可以缩小代码并做其他一些技巧.但是在Facebook中,如果您尝试检查元素或尝试调试,则会在控制台中收到消息,您无法看到任何代码.我怎样才能做到这一点?安全吗?我正在使用fire base,我使用JavaScript来获取数据和身份验证.确保我的代码受到保护非常重要.
为新秀问题道歉.我想将postgresql作为所有新rails应用程序的默认值.我知道这个命令:
rails new my_app --database=postgresql
Run Code Online (Sandbox Code Playgroud)
...但我对sqlite3和输入这个额外的命令有一种非理性的厌恶.我希望我的rails应用程序能够一夫一妻地爱postgres,而不是告诉他们他们不应该首先与sqlite3挂钩.我该怎么做?
我使用rbenv(再次,非理性)来管理我的ruby版本.提前致谢.
我有一个简单的例子,它传递的哈希值缺少字段。
我想知道哪种是添加具有默认值的字段的首选方法,以及何时选择一个是否有好处?
hash['field'] = some_value
Run Code Online (Sandbox Code Playgroud)
与
hash.merge!({ 'field' => some_value })
Run Code Online (Sandbox Code Playgroud)
编辑:打算使用合并!
我正在使用ruby将大型TXT文件加载到数组或哈希中.输入文件包含超过1'000'000个MD5哈希值,按字母顺序排序,不重复.
Ruby中最快的方法是找出我的数组或哈希中是否存在某个哈希值?目前我使用数组并"包含?".
def loadhashlist
@all_hash_values = Array.new
f = File.readlines("inputmd5.txt").each do |row|
@all_hash_values.push(row.gsub("\n",""))
end
end
loadhashlist
def hashlookup(file)
md5 = file.getMd5
if @all_hash_values.include? md5
#code goes here
end
end
Run Code Online (Sandbox Code Playgroud) 如何按字母顺序在kotlin中对以下字符串数组进行排序?
val array = arrayOf("abc","bcd","xyz","ghi","acd")
Run Code Online (Sandbox Code Playgroud)