我有这个字符串,我想知道如何将其转换为哈希.
"{:account_id=>4444, :deposit_id=>3333}"
Run Code Online (Sandbox Code Playgroud) 我想按月对帖子进行分页,所以我在Post模型中添加了以下范围
class Post
include Mongoid::Document
include Mongoid::Timestamps
scope :by_month, lambda {|end_date| Post.order_by(:created_at => :asc).where(:created_at.gte => (end_date.to_date.beginning_of_month), :created_at.lte => (end_date.to_date))}
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我放了
def show
@posts = Post.by_month(Time.now).page(params[:page]).per(20)
end
Run Code Online (Sandbox Code Playgroud)
在视野中
<%= paginate @posts, :theme => 'month_theme' %>
<%= render @posts %>
Run Code Online (Sandbox Code Playgroud)
问题:
我正在使用ruby 1.9来解析以下带有MacRoman字符的csv文件
# encoding: ISO-8859-1
#csv_parse.csv
Name, main-dialogue
"Marceu", "Give it to him ó he, his wife."
Run Code Online (Sandbox Code Playgroud)
我做了以下解析这个.
require 'csv'
input_string = File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")
#=> "Name, main-dialogue\r\n\"Marceu\", \"Give it to him \x97 he, his wife.\"\r\n"
data = CSV.parse(input_string, :quote_char => "'", :col_sep => "/\",/")
#=> [["Name, main-dialogue"], ["\"Marceu", " \"Give it to him \x97 he, his wife.\""]]
Run Code Online (Sandbox Code Playgroud)
所以,问题是数据中的第二个数组是单个字符串而不是2个字符串,如:
["\"Marceu\"", " \"Give it to him \x97 he, his wife.\""]]
我试过:col_sep => ","(这是默认行为),但它给了我3个分裂.
header = CSV.parse(input_string, :quote_char …Run Code Online (Sandbox Code Playgroud)