是否存在Python等效于Ruby ||=
运算符("如果变量未设置则设置变量")?
Ruby中的示例:
variable_not_set ||= 'bla bla'
variable_not_set == 'bla bla'
variable_set = 'pi pi'
variable_set ||= 'bla bla'
variable_set == 'pi pi'
Run Code Online (Sandbox Code Playgroud) 我喜欢Python中的一个名为"请求"的库.请求是在urllib3上构建的HTTP客户端." 请求doc ".
我在Ruby中寻找类似的东西.基本上我需要的是:
我看过Typhoeus,但主页中的代码示例不起作用; 他们已经移动了代码并且get
方法不再像这样直接访问,因此它不能很好地启动.Curb似乎很好,我喜欢cURL,还有rest-client,这看起来很受欢迎,而em-http根据基准测试似乎相当快.还有一个Patron和curb-fu,我没有时间去尝试.当然还有Net:HTTP.但是,似乎并没有每个人都指出的主流解决方案.
我想很多人都在我的情况下,我想知道他们选择了什么,为什么?
我最近运行更新:
gem update --system
gem update
Run Code Online (Sandbox Code Playgroud)
现在,每次加载gem时,我都会收到很多弃用警告.例如,rails console
:
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/user/.rvm/gems/ruby-1.9.2-p180@global/specifications/rake-0.8.7.gemspec:10.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/user/.rvm/gems/ruby-1.9.2-p180@global/specifications/rake-0.8.7.gemspec:10.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/user/.rvm/gems/ruby-1.9.2p180@global/specifications/rake-0.8.7.gemspec:10.
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 > exit
Run Code Online (Sandbox Code Playgroud)
我使用RVM,Ruby 1.9.2和Rubygems 1.8.1.有什么方法可以解决这个问题吗?还原到旧版本的rubygems?
我现在正在攻击Rails一年半,我非常喜欢它!:)
在rails中,我们大量使用局部变量,实例变量(如@user_name
)和初始化器中定义的常量(如FILES_UPLOAD_PATH
).但为什么没有人使用全球"美元化"变量($)$dynamic_cluster_name
?
是因为设计缺陷吗?它与性能有关吗?安全漏洞?
我有一些问题使用has_one, through => model
.最好的是告诉你我的情况.
class Category
has_many :articles
end
class Article
has_many :comments
belongs_to :category
end
class Comment
belongs_to :article
has_one :category, :through => :articles
end
Run Code Online (Sandbox Code Playgroud)
Everthing工作正常.我能做到comment.category
.问题是当我创建新评论并设置其文章时,我已保存评论以使关联有效.示例:
>> comment = Comment.new
>> comment.article = Article.last
>> comment.category
-> nil
>> comment.article.category
-> the category
>> comment.save
>> comment.category
-> nil
>> comment.reload
>> comment.category
-> the category
Run Code Online (Sandbox Code Playgroud)
has_one, through => model
无论如何不要设置,构建构造函数和创建方法.所以,我想通过以下方式替换我的评论模型:
class Comment
belongs_to :article
def category
article.category
end
end
Run Code Online (Sandbox Code Playgroud)
听起来不错?
我不知道为什么人们似乎更喜欢:input
在input
一个jQuery选择?基本上,这两行似乎做同样的事情:
$('input:first').focus()
$(':input:first').focus()
Run Code Online (Sandbox Code Playgroud)
但第二版更广泛使用,我找不到原因.而且,:input
根据这个基准测试选择器似乎更慢:http://jsperf.com/input-vs-input/2
使用非标准的utf-8字符时,我的验证正则表达式有问题.所以,我进行了一些实验,看来ruby正则表达式在有rails环境或普通红宝石时表现不同.
我在这里发布了一个中文字符串.
在红宝石"纯":
string = "???"
puts string[/\A[\w]*\z/]
=> match "???" - ok
Run Code Online (Sandbox Code Playgroud)
在铁轨中:
# coding: utf-8
task :test => :environment do
string = "???"
puts string[/\A[\w]*\z/]
end
$ rake test
=> nothing - not ok
Run Code Online (Sandbox Code Playgroud)
如果我省略# coding: utf-8
,它会随之而来invalid multibyte char (US-ASCII)
.无论如何,即使这样,它也不匹配.
当然,我已经检查了一切(ruby_version,utf-8中脚本文件的编码..)
我用 :
所以我的结论是,rails改变了正则表达式的行为方式,而且我没有找到一种方法让它在正常的ruby中表现得像.
我知道,数据库设计确实很差,但是在这里,我有一个论坛平台(基于PunBB),并且对于每个论坛,我都生成了一组新表。真是个坏主意。
时间过去了,现在我有超过100,000张桌子(SHOW TABLES; SELECT FOUND_ROWS();
- 112965 rows in set (1.21 sec)
)。尽管表可以完成索引工作,并且直接引用一个表时,性能却非常好,因此性能非常好。
现在的问题是我正在尝试备份所有内容并移至另一台服务器。当然,这需要永远。我已经启动了mysqldump
:
mysqldump --max_allowed_packet=500M --force --opt -u root -pXXXX a > fullbackup.sql
Run Code Online (Sandbox Code Playgroud)
而且仍在处理中,超过12小时!备份大小已经为546 Mb,Mysql仍然有效并且可以正常工作。
我试图直接复制Mysql文件,但是遇到了很多表已损坏的问题。
有什么想法可以加快速度吗?
我们目前正在使用postgresql作为我们在rails中的生产数据库,很棒的数据库,但我正在围绕SQLite构建我们的应用程序的新版本.实际上,我们不使用postgres的高级功能,如全文搜索或PL/SQL.考虑到SQLite,我喜欢用一个文件移动数据库的想法,它在服务器和Rails中的简单集成,性能似乎非常好 - > Benchmark
我们的应用程序的流量相对较高,我们每天的观看次数达到了120万次.因此,我们从数据库中进行了大量读取,但是我们做了一些写操作.
你对那个怎么想的 ?任何使用或尝试(像我们)使用SQLite像生产数据库的人的反馈?
我们有一个很大的rails应用程序,并且几天之后,我们的ruby进程似乎在一个循环中阻塞并吃100%的CPU,直到乘客服务器死于502错误.
你知道找出原因的最佳方法吗?
我尝试过New Relic,但它只是性能的东西,我们的错误太多,无法猜测是什么问题.(我们每天都有很多请求和很多UTF-8 BSON错误,因为我们使用的是UTF-8网址)
使用: