if __FILE__ == $0
$:.unshift File.join(File.dirname(__FILE__),'..')
Run Code Online (Sandbox Code Playgroud)
我在Ruby中发现了这个代码,这是什么意思?
嘿大家,我想知道行业中哪些方法是Rails中的浏览器检测标准?是否有某个宝石,库或示例代码可以帮助确定浏览器并将类或ID应用于(X)HTML的body元素?谢谢,我只是想知道每个人使用什么以及是否有可接受的方法来做到这一点?
我知道我们可以获取user.agent并解析该字符串,但我不确定这是否是一种可接受的浏览器检测方式.
另外,我不打算在这里讨论功能检测,我已经在StackOverflow上阅读了多个答案,我要求的就是你们所做的.
[UPDATE]
所以感谢GitHub上的faunzy,我对Rails中的用户代理进行了一些了解,但是仍然不确定这是否是在Rails 3中进行此操作的最佳方式.但这就是我所得到的远:
def users_browser
user_agent = request.env['HTTP_USER_AGENT'].downcase
@users_browser ||= begin
if user_agent.index('msie') && !user_agent.index('opera') && !user_agent.index('webtv')
'ie'+user_agent[user_agent.index('msie')+5].chr
elsif user_agent.index('gecko/')
'gecko'
elsif user_agent.index('opera')
'opera'
elsif user_agent.index('konqueror')
'konqueror'
elsif user_agent.index('ipod')
'ipod'
elsif user_agent.index('ipad')
'ipad'
elsif user_agent.index('iphone')
'iphone'
elsif user_agent.index('chrome/')
'chrome'
elsif user_agent.index('applewebkit/')
'safari'
elsif user_agent.index('googlebot/')
'googlebot'
elsif user_agent.index('msnbot')
'msnbot'
elsif user_agent.index('yahoo! slurp')
'yahoobot'
#Everything thinks it's mozilla, so this goes last
elsif user_agent.index('mozilla/')
'gecko'
else
'unknown'
end
end
return @users_browser
end
Run Code Online (Sandbox Code Playgroud) 在Ruby 1.9.2on中Rails 3.0.3,我试图测试两个Friend(类继承自ActiveRecord::Base)对象之间的对象相等性.
对象相等,但测试失败:
Failure/Error: Friend.new(name: 'Bob').should eql(Friend.new(name: 'Bob'))
expected #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
got #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
(compared using eql?)
Run Code Online (Sandbox Code Playgroud)
只是为了笑容,我还测试了对象身份,它失败了,正如我所料:
Failure/Error: Friend.new(name: 'Bob').should equal(Friend.new(name: 'Bob'))
expected #<Friend:2190028040> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
got #<Friend:2190195380> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
Compared using …Run Code Online (Sandbox Code Playgroud) 在Ruby中开发gem时,我几乎总是需要一个文件,我可以在其中配置RSpec以满足我的需要,也许在此之前,需要一些辅助模块,这些模块应该在我的所有spec示例中都可用.
在Rails应用程序中,使用名为的文件spec/spec_helper.rb.令我恼火的一件事是,在典型的Rails环境中,您必须在每个包含要加载示例的文件中要求此spec_helper.rb文件.在过去,我遇到了很多问题,这些问题与更改示例文件中的加载路径和相对需求路径有关.
现在,对于我的宝石,我希望有一种方法可以在加载任何示例文件之前说RSpec要求spec_helper.rb文件.如果我调用rspec可执行文件,或者我可以在我的Rakefile中定义的rake规范任务,则独立于这个事实.
我知道我只能告诉RSpec我的spec_helper.rb的位置是这个spec_helper.rb手动需要所有的示例文件,但我还想避免额外维护该方法.
有没有更好的方法来实现这一目标?
你能用ruby做到这一点吗?它似乎"错过"了不平等的案件
case myvar
when myvar < -5
do somethingA
when -5..-3
do special_something_XX
when -2..-1
do special_something_YY
when myvar == 0
do somethingB
when myvar > 0
go somethingC
end
Run Code Online (Sandbox Code Playgroud) %w[ ] Non-interpolated Array of words, separated by whitespace
%W[ ] Interpolated Array of words, separated by whitespace
Run Code Online (Sandbox Code Playgroud)
用法:
p %w{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %W{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %w{C:\ C:\Windows} # => ["C: C:\\Windows"]
p %W{C:\ C:\Windows} # => ["C: C:Windows"]
Run Code Online (Sandbox Code Playgroud)
我的问题是......有什么区别?
现在我正在做一个split字符串,并假设用户的换行符是\r\n这样的:
string.split(/\r\n/)
Run Code Online (Sandbox Code Playgroud)
我想做的就是分裂\r\n或者只是分开\n.
那么正则表达式将如何拆分其中任何一个?
我一直在寻找解释为什么Twitter必须将部分中间件从Rails迁移到Scala的原因.是什么阻止了他们扩展Facebook的方式,通过添加服务器作为其用户群扩展.更具体地说,Ruby/Rails技术阻止了Twitter团队采用这种方法?
我有一些由中国机器人触发的错误:http://www.easou.com/search/spider.html滚动我的网站时.
我的应用程序的版本都使用Ruby 1.9.3和Rails 3.2.X
这里是一个堆栈跟踪:
An ArgumentError occurred in listings#show:
invalid byte sequence in UTF-8
rack (1.4.5) lib/rack/utils.rb:104:in `normalize_params'
-------------------------------
Request:
-------------------------------
* URL : http://www.my-website.com
* IP address: X.X.X.X
* Parameters: {"action"=>"show", "controller"=>"listings", "id"=>"location-t7-villeurbanne--58"}
* Rails root: /.../releases/20140708150222
* Timestamp : 2014-07-09 02:57:43 +0200
-------------------------------
Backtrace:
-------------------------------
rack (1.4.5) lib/rack/utils.rb:104:in `normalize_params'
rack (1.4.5) lib/rack/utils.rb:96:in `block in parse_nested_query'
rack (1.4.5) lib/rack/utils.rb:93:in `each'
rack (1.4.5) lib/rack/utils.rb:93:in `parse_nested_query'
rack (1.4.5) lib/rack/request.rb:332:in `parse_query'
actionpack (3.2.18) lib/action_dispatch/http/request.rb:275:in `parse_query'
rack (1.4.5) …Run Code Online (Sandbox Code Playgroud) 使用最新版本的haml,haml-rails,sass和bootstrap-sass运行Rails 4.1.4.对于用户显示,我的HAML是这样的:
.tutors-listing
.row
- @users.each do |tutor|
.col-xs-12.col-md-3
.row.tutor
.col-xs-offset-1.col-xs-4.col-md-12
= image_tag tutor.photo, :class => 'img-responsive img-circle tutor-photo'
%h4.tutor-name
= tutor.first_name
%p
teaches
%strong.tutor-skills
= tutor.teachables
Run Code Online (Sandbox Code Playgroud)
但是,此标记会导致以下故障:

我希望有人可以解决这里的错误.在中断点处,应该有4列均匀.