rails提供了相当方便的扩展,你可以这样实现:
在application.rb中加入:
config.paths["config/routes"] += Dir[Rails.root.join("config/routes/*.rb")]在config中创建routes文件夹,将mobile_routes.rb放到routes文件夹下即可,rails会自动将routes文件夹下的文件加载。mobile_routes.rb文件的写法与routes.rb文件一样!
config.paths["config/routes"] += Dir[Rails.root.join("config/routes/*.rb")]在config中创建routes文件夹,将mobile_routes.rb放到routes文件夹下即可,rails会自动将routes文件夹下的文件加载。ruby-1.9.2-p180 :055 > File.join("lib/","ruby","gem.rb")
=> "lib/ruby/gem.rb"
ruby-1.9.2-p180 :058 > [1,2,3] - [1,2]
=> [3]
ruby-1.9.2-p180 :059 > [1,2,3] & [1,2]
=> [1, 2]
ruby-1.9.2-p180 :060 > [1,2,3] + [1,2,3,4]
=> [1, 2, 3, 1, 2, 3, 4]
ruby-1.9.2-p180 :066 > %w(a d c d 1 2 3)
=> ["a", "d", "c", "d", "1", "2", "3"]
ruby-1.9.2-p180 :074 > Hash["a",1,"b",2]
=> {"a"=>1, "b"=>2}
ruby-1.9.2-p180 :075 > Hash[*["a",1,"b",2]]
=> {"a"=>1, "b"=>2}
ruby-1.9.2-p180 :075 > Array("a".."z").select.with_index {|x,i| i%3 == 2}
=> ["c", "f", "i", "l", "o", "r", "u", "x"]
# coding:utf-8 # unicode转义 "" << 0x6f22 #=> 漢
class Object
# returns instance methods of current object's class and its singleton methods
def lm # abbr of local methods
self.methods.sort - self.class.superclass.instance_methods
end
endA utility method for escaping HTML tag characters. This method is also aliased as h.
In your ERB templates, use this method to escape any unsafe content. For example:
<%=h @person.name %>
puts html_escape("is a > 0 & a < 10?") # => is a > 0 & a < 10?
Source: hide
# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 18 def html_escape(s) s = s.to_s if s.html_safe? s else s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<").html_safe end end
Escape carrier returns and single and double quotes for JavaScript segments. Also available through the alias j(). This is particularly helpful in JavaScript responses, like:
$('some_element').replaceWith('<%=j render 'some/element_template' %>');
Source: hide
# File actionpack/lib/action_view/helpers/javascript_helper.rb, line 19 def escape_javascript(javascript) if javascript result = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] } javascript.html_safe? ? result.html_safe : result else '' end end
%w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
["1", "2", "3"]
["4", "5", "6"]
["7", nil, nil]
%w(1 2 3).in_groups_of(2, ' ') {|g| p g}
["1", "2"]
["3", " "]
%w(1 2 3).in_groups_of(2, false) {|g| p g}
["1", "2"]
["3"]
<table>
<% @tasks.in_groups_of(4) do |row_tasks| %>
<tr>
<% for task in row_tasks %>
<td><%= task.name %></td>
<% end %>
</tr>
<% end %>
</table>