我看到一些库方法中使用的哈希参数,正如我一直在学习的那样.
例如,
list.search(:titles, genre: 'jazz', duration_less_than: 270)
Run Code Online (Sandbox Code Playgroud)
有人可以解释一个方法如何使用这样的参数,以及如何创建一个使用哈希参数的方法?
我知道如何写入文件,并从文件中读取,但我不知道如何修改文件,除了将整个文件读入内存,操作它,并重写整个文件.对于大文件,这不是很有效率.
我真的不知道追加和写的区别.
例如
如果我有一个文件包含:
Person1,will,23
Person2,Richard,32
Person3,Mike,44
Run Code Online (Sandbox Code Playgroud)
我怎么能只删除包含Person2的行?
我不明白如下:
counts = Hash.new(0)
File.foreach("testfile") do |line|
line.scan(/\w+/) do |word|
word = word.downcase
counts[word] += 1
end
end
counts.keys.sort.each {|k| print "#{k}:#{counts[k]} "}
Run Code Online (Sandbox Code Playgroud)
比这更糟糕的是:
words = Fiber.new do
File.foreach("testfile") do |line|
line.scan(/\w+/) do |word|
Fiber.yield word.downcase
end
end
end
counts = Hash.new(0)
while word = words.resume
counts[word] += 1
end
counts.keys.sort.each {|k| print "#{k}:#{counts[k]} "}
Run Code Online (Sandbox Code Playgroud) 我有以下来自Ruby 1.9编程的代码(略有改编),我只是想确保自己的想法正确无误
module Trace
def self.included(culprit)
#Inject existing methods with tracing code:
culprit.instance_methods(false).each do |func|
inject(culprit, func)
end
#Override the singletons method_added to ensure all future methods are injected.
def culprit.method_added(meth)
unless @trace_calls_internal
@trace_calls_internal = true
Trace.inject(self, meth) #This will call method_added itself, the condition prevents infinite recursion.
@trace_calls_internal = false
end
end
end
def self.inject(target, meth)
target.instance_eval do
#Get the method
method_object = instance_method(meth)
#Rewrite dat sheet
define_method(meth) do |*args, &block|
puts "==> Called #{meth} with #{args.inspect}"
#the bind …
Run Code Online (Sandbox Code Playgroud) # Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Dummy::Application.load_tasks
Run Code Online (Sandbox Code Playgroud)
鉴于上述情况,在制作引擎时,这个虚拟应用程序也会加载位于 /lib/tasks 中的引擎任务。它是如何做到这一点的?我似乎无法找到 load_tasks 的定义位置或它是如何工作的..
任何建议都会很棒。
我一直在寻找解决这个问题的方方面面.我已经在我的gemfile中将jquery-rails gem降级为2.3.0.但是当我在application.js中包含jquery-ui时,我仍然会收到Sprokets :: FileNotFound错误.
couldn't find file 'jquery-ui'
(in /home/richard/projects/pickaxe/mini-projects/depot-app/app/assets/javascripts/application.js:14)
Run Code Online (Sandbox Code Playgroud)
这是我的application.js文件:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled …
Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以在twitter bootstrap btn-group-vertical按钮之间添加一些边距,并使它们都有圆角?
我的第一次尝试是在他们之间添加一些余地,使用css,但后来我看到了,除了第一个和最后一个,没有一个有圆角,所以不是"玩",我希望有一个"体面的"实现这一目标的方式......
给定此数组(从文件生成)
["Yonkers", "DM1210", "70.00 USD"], ["Yonkers", "DM1182", "19.68 AUD"],
["Nashua", "DM1182", "58.58 AUD"], ["Scranton", "DM1210", "68.76 USD"],
["Camden", "DM1182", "54.64 USD"]]
Run Code Online (Sandbox Code Playgroud)
我将它转换为由第二个元素(sku)索引的哈希,代码如下:
result = Hash.new([])
trans_data.each do |arr|
result[arr[1]].empty? ? result[arr[1]] = [[arr[0], arr[2]]] : result[arr[1]] << [arr[0], arr[2]]
end
result
Run Code Online (Sandbox Code Playgroud)
这以我想要的格式输出哈希:
{"DM1210"=>[["Yonkers", "70.00 USD"], ["Scranton", "68.76 USD"]], "DM1182"=>[["Yonkers", "19.68 AUD"], ["Nashua", "58.58 AUD"], ["Camden", "54.64 USD"]]}
Run Code Online (Sandbox Code Playgroud)
我不觉得我的代码是......干净.有没有更好的方法来实现这一目标?
编辑:到目前为止,我能够用以下代码替换它: (result[arr[1]] ||= []) << [arr[0], arr[2]]
没有哈希的默认值
我正在尝试制作一个基本的CSS按钮。
尝试在按钮内垂直和水平居中放置文本时遇到错误。我有下面的HTML。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#button
{
background:lightblue;
border-radius: 30px;
width: 15em;
height: 3em;
position:relative;
}
h1 {
vertical-align: center;
text-align: center;
}
</style>
<title>Making a button...</title>
</head>
<body>
<div id="button"><h1>Click Me</h1></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
jsfiddle用于此问题:http : //jsfiddle.net/CRpvr/
有人告诉我有一种使用的变通办法line-height: 100%
,但如果我需要多个行,则效果不佳。什么是最惯用且恰当的方法来完成此任务?
我正在努力理解何时使用&符号将符号传递给表示方法的函数。例如,如果我想计算范围 1..10 的总和,我可以执行以下操作:
(1..10).inject(:+)
这最初让我相信,如果你想传递一个符号来定义一个方法来“神奇地”在函数中使用,你可以将函数名称作为符号传递。但是后来我在 Rails 中看到了类似的东西:
total = Product.find(product_list).sum(&:price)
如果我理解正确,&:price 与调用 :price.to_proc 相同。我不明白上面是如何工作的。
ruby symbols function-pointers metaprogramming ruby-on-rails