这是排序哈希并返回哈希对象(而不是数组)的最佳方法:
h = {"a"=>1, "c"=>3, "b"=>2, "d"=>4}
# => {"a"=>1, "c"=>3, "b"=>2, "d"=>4}
Hash[h.sort]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
Run Code Online (Sandbox Code Playgroud) 我需要将"/ [\ w\s] + /"之类的字符串转换为正则表达式.
"/[\w\s]+/" => /[\w\s]+/
Run Code Online (Sandbox Code Playgroud)
我尝试使用不同的Regexp方法,如:
Regexp.new("/[\w\s]+/") => /\/[w ]+\//,同样Regexp.compile和Regexp.escape.但是没有一个像我预期的那样回归.
我还尝试删除反斜杠:
Regexp.new("[\w\s]+") => /[w ]+/ 但没有运气.
然后我试着这么简单:
str = "[\w\s]+"
=> "[w ]+"
Run Code Online (Sandbox Code Playgroud)
它逃脱了.现在,字符串如何保持不变并转换为正则表达式对象?
更新XCode 8.2后,每次保存模拟器屏幕截图总是让它崩溃.即使我重置内容和设置仍然崩溃.
我应该删除所有模拟器并重新安装吗?
更新:
以下是崩溃报告:
Process: Simulator [48928]
Path: /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator
Identifier: com.apple.iphonesimulator
Version: 10.0 (726.7)
Build Info: Indigo-726007000000000~5
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Simulator [48928]
User ID: 501
PlugIn Path: /Applications/Xcode.app/Contents/Frameworks/libswiftFoundation.dylib
PlugIn Identifier: libswiftFoundation.dylib
PlugIn Version: 3.0.2 (800.0.63)
Date/Time: 2016-12-13 23:43:25.860 +0800
OS Version: Mac OS X 10.11.6 (15G1108)
Report Version: 11
Anonymous UUID: 6E8AEBAE-0AF8-D350-66D6-57EC500D69F5
Sleep/Wake UUID: 6E017B49-1A38-4D3A-AB8F-FD9E200B6286
Time Awake Since Boot: 110000 seconds
Time Since Wake: 10000 seconds
System Integrity Protection: enabled …Run Code Online (Sandbox Code Playgroud) 我试图找到一些解决方案,但我真的找不到与运行rails命令时出现的错误相关的任何内容:
rails generate model书名:string summary:text isbn:string
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Running via Spring preloader in process 3579
Expected string default value for '--jbuilder'; got true (boolean)
invoke active_record
identical db/migrate/20170104114702_create_books.rb
identical app/models/book.rb
invoke test_unit
identical test/models/book_test.rb
identical test/fixtures/books.yml
Run Code Online (Sandbox Code Playgroud)
有谁知道可能导致这些错误的原因?
我试图让Ruby调试器在我的一个规范中运行:
describe User do
it "should be valid" do
debugger
User.new.should be_valid
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行rspec时,我得到:
debugger statement ignored, use -d or --debug option to enable debugging
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
rake spec --debug
rake spec --debug --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec …Run Code Online (Sandbox Code Playgroud) str = "Hello? World?"
Run Code Online (Sandbox Code Playgroud)
预期产出是:
"Hello:) World:("
Run Code Online (Sandbox Code Playgroud)
我可以做这个: str.gsub("?", ":)").gsub("?", ":(")
有没有其他方法可以在单个函数调用中执行此操作?就像是:
str.gsub(['s1', 's2'], ['r1', 'r2'])
Run Code Online (Sandbox Code Playgroud) 在Ruby中,我可以做到('a'..'z').to_a并且得到['a', 'b', 'c', 'd', ... 'z'].
jQuery或Javascript提供类似的构造吗?
如何在Ruby中的字符串的两个字符串标记之间返回字符串?
例如,我有:
input_string str1_markerstringstr2_markerstring想做类似的事情:
input_string.string_between_markers(str1_markerstring, str2_markerString)
Run Code Online (Sandbox Code Playgroud)
示例文字:
s
# => "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"
str1_markerstring
# => "Charges for the period"
str2_markerstring
# => "Any Network Cap"
s[/#{str1_markerstring}(.*?)#{str2_markerstring}/, 1]
# => nil # IE DIDN'T WORK IN THIS CASE
Run Code Online (Sandbox Code Playgroud) 这可能是一个简单的问题,但我似乎正在拔头发找到一个优雅的解决方案.我有两个ActiveRecord模型类,它们之间有一个has_one和belongs_to关联:
class Item < ActiveRecord::Base
has_one :purchase
end
class Purchase < ActiveRecord::Base
belongs_to :item
end
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种优雅的方法来查找所有Item对象,这些对象没有与它们关联的购买对象,理想情况下不需要is_purchased在Item上使用布尔值或类似属性.
现在我有:
purchases = Purchase.all
Item.where('id not in (?)', purchases.map(&:item_id))
Run Code Online (Sandbox Code Playgroud)
哪个有效,但对我来说似乎效率低下,因为它正在执行两个查询(并且购买可能是一个巨大的记录集).
运行Rails 3.1.0
当我尝试启动mySQL服务器时,我收到消息:
[警告]不推荐使用具有隐式DEFAULT值的TIMESTAMP.
请使用--explicit_defaults_for_timestamp服务器选项(有关详细信息,请参阅文档).
我找到答案:http:
//dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
但是如何启用呢?哪里?
ruby ×6
regex ×2
string ×2
activerecord ×1
gsub ×1
hashmap ×1
ios ×1
javascript ×1
jquery ×1
mysql ×1
rake ×1
rspec ×1
ruby-1.9.3 ×1
ruby-debug ×1
sorting ×1
xcode ×1