下面的函数试图寻找nth到最后一个单向链表的元素.
例如:
如果元素是8->10->5->7->2->1->5->4->10->10结果是
7th最后一个节点是7.
任何人都可以帮助我解释这段代码是如何工作的,还是有更好更简单的方法?
LinkedListNode nthToLast(LinkedListNode head, int n) {
if (head == null || n < 1) {
return null;
}
LinkedListNode p1 = head;
LinkedListNode p2 = head;
for (int j = 0; j < n - 1; ++j) { // skip n-1 steps ahead
if (p2 == null) {
return null; // not found since list size < n
}
p2 = p2.next;
}
while (p2.next != …Run Code Online (Sandbox Code Playgroud) 我正在为rails 3应用程序创建一个新引擎.您可以猜到,此引擎位于我的应用程序的lib目录中.
但是,我在开发它时遇到了一些问题.实际上,每次我更改引擎中的内容时,我都需要重新启动服务器.
有办法避免这种情况吗?
我可以强制rails完全重新加载lib目录或特定文件以及他对每个请求的要求吗?
谢谢你的帮助 :)
此代码取自ActiveRecord 2.3.14的gem类 ConnectionHandler
def establish_connection(name, spec)
@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
end
Run Code Online (Sandbox Code Playgroud)
似乎每次ruby调用establish_connection模型时,它都会创建一个新的连接池.
我的问题:
如果我有5个模型用于establish_connection同一个数据库,Rails是否足够聪明,可以选择一个已经存在的池,而不是创建一个具有相同连接凭据的新池?如果我的5个模型是一些使用的抽象类的子类,是否也会发生这种情况establish_connection?是否总是从@connection_pools它存在的地方选择一个连接?
更新1
我在谈论一个具体的例子.你有5个模型有5个不同的连接,每次Rails使用它执行的模型establish_connection.查看ActiveRecord中的代码,当它执行时,establish_connection它会创建一个与该特定连接有连接的新池.我想知道的是,每次Rails调用模型时establish_connection,是创建新池还是采用现有池.
示例:您访问我的网站并查看产品列表.您刚刚点击了一个调用的动作,该动作Product.all执行establish_connection到亚马逊上的某个数据库.然后,我来到产品列表,会发生什么?我是否已获取已建立的连接,或者是否正在创建具有该连接的新池?
更新2
我的猜测是,第一次Rails加载我的模型,它创建了具有不同连接的池.之后,当我使用一些时Model.method,它只是抓取与模型相关的连接并执行该方法.
我不确定当2个模型有两个相等的连接时会发生什么(不是在抽象类中而是在自习类中).这会产生两个相同的连接池,还是ActiveRecord足够智能来捕捉这种情况?
我经常在Ruby on Rails的上下文中听到"中间件"这个术语.究竟是什么?你能提供具体的例子吗?
我试图用haml编写这个html,以便在项目是当前项目时添加id标记.这是为jquery突出显示设置.
<% if line_item == @current_item %>
<tr class="line_item id="current_item">
<% else %>
<tr class="line_item">
<% end %>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
因为我不想编写辅助方法,所以我将if语句粘贴在标记中:
%tr.line_item{ :id => (line_item == @current_item ? '' : 'current_item') }
%td
= line_item.quantity
%td
\x #{line_item.product.title}
%td.item_price
= number_to_currency(line_item.total_price)
%td.item_remove
= button_to 'Remove', line_item, :method => :delete
Run Code Online (Sandbox Code Playgroud)
但是,'current_item'的这个id标签包含所有项目而不仅仅是当前项目.这导致javascript突出显示所有或错误的条目.关于如何让haml合作的想法?
当我尝试运行'rails server'时,它给我一个错误,说它找不到'jcode',我认为jcode是一个默认的ruby lib.你们有什么线索关于什么事情发生?
/Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/gdata-1.1.1/lib/gdata.rb:21:in `require': no such file to load -- jcode (LoadError)
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/gdata-1.1.1/lib/gdata.rb:21:in `<top (required)>'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/contacts-1.2.4/lib/contacts/gmail.rb:1:in `require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/contacts-1.2.4/lib/contacts/gmail.rb:1:in `<top (required)>'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/contacts-1.2.4/lib/contacts.rb:6:in `require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/contacts-1.2.4/lib/contacts.rb:6:in `<top (required)>'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:64:in `require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:64:in `block (2 levels) in require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:62:in `each'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:62:in `block in require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:51:in `each'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler/runtime.rb:51:in `require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.3/lib/bundler.rb:112:in `require'
from /Users/seanfchan/RubyPractice/gettingContancts/config/application.rb:7:in `<top (required)>'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/railties-3.0.1/lib/rails/commands.rb:28:in `require'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/railties-3.0.1/lib/rails/commands.rb:28:in `block in <top (required)>'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/railties-3.0.1/lib/rails/commands.rb:27:in `tap'
from /Users/seanfchan/.rvm/gems/ruby-1.9.2-p0@global/gems/railties-3.0.1/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in …Run Code Online (Sandbox Code Playgroud) 在"显示"页面中有一些用于就地编辑模型的选项,即无需在"编辑"页面中加载表单.例如,请参阅http://www.ruby-toolbox.com/categories/rails_in_place_editing.html.
有没有人在Rails 3中使用任何这些选项(或其他)的经验?任何指针或建议?
就我而言,我有一个相当长的形式,由可变数量的项目组成.从可用性的角度来看,在同一页面中编辑这些项目中的文本是很有意义的,而不需要为将每个用户发送到特定项目的编辑页面的每个项目的编辑按钮.
我正在寻找其他人通常如何组织他们的部分多态资源.
例:
我有多Image态,取决于什么imageable,我想显示略有不同的东西.
我有一个部分images/_image,可以打电话render imageable.images.我目前的心态是让我的图像部分检查是什么类型imageable,然后有另一部分,特定于该情况.我的组织将是这样的:
images/
_image.html.haml
_product.html.haml
_post.html.haml
_user.html.haml
Run Code Online (Sandbox Code Playgroud)
我的_image部分看起来像:
%div
= render :partial => "images/#{imageable.type}"
Run Code Online (Sandbox Code Playgroud)
这看起来像是一种糟糕的方法,还是采取了错误的做法?我认为render imageable.images从任何地方打电话比在render :partial => ...全场打电话要好得多.
任何想法将不胜感激.你是怎么做到的?
编辑:很长一段时间过去了,我仍然想知道是否有人对此有任何意见.抛出赏金,看看是否引起了一些注意.
有没有办法检索我的ruby连接的数据库连接字符串?我想得到的是:
1)连接ruby的数据库名称2)SQL Server的用户名3)SQL Server的密码4)服务器名称
我想将它存储在会话变量中.
(我使用的是MS SQL Server.)
请帮忙!谢谢!
activerecord ×2
ruby ×2
ajax ×1
algorithm ×1
haml ×1
if-statement ×1
linked-list ×1
organization ×1
reload ×1
rubygems ×1