我使用了在 Rails 中使用 Javascript指南中的remote: true习语:
# new.html.slim
= form_for @thing, remote: true do |f|
f.text_field :whatever
f.submit 'Submit'
Run Code Online (Sandbox Code Playgroud)
# thing_controller.rb
layout 'foo'
def create
end
Run Code Online (Sandbox Code Playgroud)
# create.js.erb
alert('foobar')
Run Code Online (Sandbox Code Playgroud)
这失败了,因为由于create.js.erb某种原因在 'foo' 布局内呈现并返回为 html,而不是 javascript,尽管该请求被正确处理为 Javascript:
Processing by ThingsController#create as JS
Parameters: {"utf8"=>"?", "commit"=>"Submit"}
Rendered things/create.js.erb (0.6ms)
Run Code Online (Sandbox Code Playgroud)
(无论我respond_to在控制器操作中是否有显式格式块,问题都是一样的。)
如此处和此处所述,包括render layout: false在控制器操作中解决了问题:
# thing_controller.rb
layout 'foo'
def create
render layout: false
end
Run Code Online (Sandbox Code Playgroud)
但为什么我需要render layout: false这里?为什么 Rails 在 …
我正在使用Ruby 2.2.2,Pry和'pry-byebug'.该continue语句删除了所有的监视变量pry-byebug:
[1] pry(main)> watch foo
Watching foo
watch: foo => 42
[2] pry(main)> watch
Listing all watched expressions:
1: foo => 42
[3] pry(main)> continue
[1] pry(main)> watch
No watched expressions
Run Code Online (Sandbox Code Playgroud)
每次丢失它们都会continue让看到的表情变得毫无价值.如果我使用next并代替代码中的相同点,则观察到的表达式仍然存在; 只是continue导致问题的原因.目前我甚至无法在watch声明中找到任何文档,所以我不知道为什么会这样.
更一般地说,我只想在每个Pry提示符上打印出一组表达式的值(或者,如果它们自上次打印以来它们已经改变了,可能会将它们打印出来.)如何在Ruby中实现这一点调试器?
假设我想向我的(启用 yargs 的)脚本传递一个布尔选项:
./foo --secure-but-slow=false
Run Code Online (Sandbox Code Playgroud)
secure-but-slow应默认为 true,因此只有知道自己在做什么的用户才会选择不安全的选项。然而,对于高级用户来说,有一个快捷方式的短选项标志会很好,例如
./foo -i
Run Code Online (Sandbox Code Playgroud)
-i告诉 yargs应该设置的正确方法是什么--secure-but-slow=false?
是否有一个 Pythontyping类用于不是映射的项目集合——即集合、列表或元组,但不是字典?即,这Unicorn存在吗?
>>> from typing import Unicorn
>>> isinstance({1, 2, 3}, Unicorn)
True
>>> isinstance([1, 2, 3], Unicorn)
True
>>> isinstance((1, 2, 3), Unicorn)
True
>>> isinstance({1: 'a', 2: 'b'}, Unicorn)
False
Run Code Online (Sandbox Code Playgroud)
Collection当然看起来很有希望,但听写也Collection很不错。
我对此感到困惑:
use itertools::Itertools; // 0.10.0
fn main() {
let combos = ["a", "b", "c"].iter().combinations(2).collect::<Vec<_>>();
println!("{:#?}", combos[0].join(""));
}
Run Code Online (Sandbox Code Playgroud)
error[E0599]: the method `join` exists for struct `Vec<&&str>`, but its trait bounds were not satisfied
--> src/main.rs:5:33
|
5 | println!("{:#?}", combos[0].join(""));
| ^^^^ method cannot be called on `Vec<&&str>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Vec<&&str>: Iterator`
which is required by `Vec<&&str>: Itertools`
`<[&&str] as Join<_>>::Output = _`
`[&&str]: Iterator`
which is required …Run Code Online (Sandbox Code Playgroud) 我发现Vim快捷方式nmap <enter> o<esc>或者nmap <enter> O<esc>用enter键插入一个空行非常有用.但是,它们会对插件造成严重破坏; 例如,ag.vim它使用要跳转到的文件名填充quickfix列表.在此窗口中按Enter键(应该跳转到文件)会给出错误E21: Cannot make changes; modifiable is off.
为了避免在quickfix缓冲区中应用映射,我可以这样做:
" insert blank lines with <enter>
function! NewlineWithEnter()
if &buftype ==# 'quickfix'
execute "normal! \<CR>"
else
execute "normal! O\<esc>"
endif
endfunction
nnoremap <CR> :call NewlineWithEnter()<CR>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我真正想要的是避免任何不可修改的缓冲区中的映射,而不仅仅是在quickfix窗口中.例如,映射在位置列表中也没有意义(并且可能会破坏使用它的其他一些插件).如何检查我是否在可修改的缓冲区中?
我有一个User,它有一个String email属性。但是,当我在应用程序中处理电子邮件时,我发现最好首先将其转换为(非持久)Email对象,如下所示:
class User < ActiveRecord::Base
def email
Email.new(self.read_attribute :email)
end
end
Run Code Online (Sandbox Code Playgroud)
Email#to_s和Email#to_str都被定义为简单的原始字符串(例如,foo@bar.com),因此对于客户端来说,无论他们正在处理 anEmail还是 a ,通常都是相当透明的String。
当使用以下方式分配属性时,这非常有效ActiveRecord:
> email = Email.new('foo@bar.com')
> user.email = email
Run Code Online (Sandbox Code Playgroud)
ActiveRecord 知道该email属性是一个字符串,并Email相应地转换该对象。有点令人费解的是,它在查询数据库时并没有这样做:
> email = Email.new('foo@bar.com')
> User.find_by email: email
ActiveRecord::StatementInvalid: can't cast Email to string
Run Code Online (Sandbox Code Playgroud)
显然,我可以打电话
> User.find_by email: email.to_s
Run Code Online (Sandbox Code Playgroud)
但是有没有办法让这个转换自动发生呢?
我经常使用begin ... end块语法来记住Ruby方法:
$memo = {}
def calculate(something)
$memo[something] ||= begin
perform_calculation(something)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这里有一个陷阱。如果我begin ... end通过保护子句从该块提早返回,则不会记录结果:
$memo = {}
def calculate(something)
$memo[something] ||= begin
return 'foo' if something == 'bar'
perform_calculation(something)
end
end
# does not memoize 'bar'; method will be run each time
Run Code Online (Sandbox Code Playgroud)
我可以通过避免以下return语句来避免这种情况:
$memo = {}
def calculate(something)
$memo[something] ||= begin
if something == 'bar'
'foo'
else
perform_calculation(something)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这可行,但是我不喜欢它,因为:
return在这种情况下我不允许使用。除了避免,还有更好的习惯用法return吗?
我有两个紧密耦合的Python类,它们需要相互引用(在类上,而不是实例上)。如何解决循环进口问题?理想情况下,我希望能够使其在同一个模块中或在两个不同的模块之间工作,但我会选择其中一个。
# yin_yang.py
class MyYin(Yin):
__yang__ = MyYang
class MyYang(Yang):
__yin__ = MyYin
Run Code Online (Sandbox Code Playgroud) 当连接到管道时,重定向在 Bash 和 Zsh 中的行为不同:
bash> echo foo >/dev/null | cat
bash>
Run Code Online (Sandbox Code Playgroud)
zsh> echo foo >/dev/null | cat
foo
zsh>
Run Code Online (Sandbox Code Playgroud)
Bash 符合我的预期;在 Zsh 中,重定向到的 STDOUT/dev/null似乎死而复生。
为什么会有这种差异?在每种情况下发生了什么?
ruby ×3
javascript ×2
python ×2
activerecord ×1
bash ×1
byebug ×1
collections ×1
debugging ×1
macvim ×1
memoization ×1
node.js ×1
pipe ×1
pry ×1
rust ×1
shell ×1
type-hinting ×1
vim ×1
viml ×1
yargs ×1
zsh ×1