阅读文档,Stream.resource似乎只是为了创建一个可以读取/获取值的资源,而不是写入/放入.我理解正确还是我读错了?如果我理解正确,我必须创建什么类型的资源才能从流中写入/放入Collectable?
目前我在做什么:
:zip.t并:zip.tt让它列出zip文件夹的内容,看看它是否是我所期待的.不知怎的,我想我错过了一些东西.测试更好:zip.table吗?该功能看起来令人困惑.有人可以举例说明如何使用它吗?下面是我得到的输出示例,但我无法弄清楚如何将其变成测试?对于zip档案来说,md5sum是一个更好的测试吗?
iex(4)> :zip.table('testing.zip')
{:ok,
[{:zip_comment, []},
{:zip_file, 'mix.exs',
{:file_info, 930, :regular, :read_write, {{2015, 7, 15}, {2, 11, 9}},
{{2015, 7, 15}, {2, 11, 9}}, {{2015, 7, 15}, {2, 11, 9}}, 54, 1, 0, 0, 0, 0,
0}, [], 0, 444},
{:zip_file, 'mix.lock',
{:file_info, 332, :regular, :read_write, {{2015, 7, 15}, {2, 9, 6}},
{{2015, 7, 15}, {2, 9, 6}}, {{2015, 7, 15}, {2, 9, 6}}, 54, 1, 0, 0, 0, 0,
0}, …Run Code Online (Sandbox Code Playgroud) 如果我正确理解 GCM 模式,它应该不仅提供加密,还提供密文的验证。然而,当我使用 Ruby 的 OpenSSL 实现以AES-256-GCM模式加密数据时,即使我篡改了auth_tag. 我在这里遗漏了一些东西还是实施确实被破坏了?
require 'openssl'
# ALICE encrypts some secret data
data = 'secret'
cipher = OpenSSL::Cipher.new('aes-128-gcm')
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv
cipher.auth_data = 'auth_data'
ciphertext = cipher.update(data) + cipher.final
auth_tag = cipher.auth_tag
# EVE tampers with the auth tag, e.g. dropping the last 10 bytes
auth_tag = auth_tag[0..-11]
# BOB decrypts the ciphertext
cipher = OpenSSL::Cipher.new('aes-128-gcm')
cipher.decrypt
cipher.key = key
cipher.iv = iv
cipher.auth_tag = auth_tag
cipher.auth_data = …Run Code Online (Sandbox Code Playgroud) 我目前正在用c ++实现二叉树,我想用一个名为in_order()的函数遍历它.
是否有任何方法可以将函数作为参数传递,这样我就可以执行以下操作(无需编写代码来遍历列表多次)?
struct tree_node; // and so on
class tree; // and so on
void print_node () {
// some stuff here
}
// some other functions
tree mytree();
// insert some nodes
mytree.in_order(print_node);
mytree.in_order(push_node_to_stack);
mytree.in_order(something_else);
Run Code Online (Sandbox Code Playgroud) 当找不到图像时,有没有办法让safari不显示"破碎的图像"符号?firefox默认这样做.
我宁愿用css这样做,但我认为javascript将是要走的路...我已经使用jquery了,例如这样的东西会很棒:
$(document).ready(function(){
$('img').broken().hide();
});
Run Code Online (Sandbox Code Playgroud) 给定嵌套文档的哈希:
myHash = {
"MemberId"=>"ABC0001",
"MemberName"=>"Alan",
"details"=>[
{"LineNumber"=>"4.1", "Item"=>"A0001", "Description"=>"Apple"},
{"LineNumber"=>"5.1", "Item"=>"A0002"},
{"LineNumber"=>"6.1", "Item"=>"Orange"}
]
}
Run Code Online (Sandbox Code Playgroud)
我想改变它所以它看起来像:
{
"memberid"=>"ABC0001",
"membername"=>"Alan",
"details"=>[
{"linenumber"=>"4.1", "item"=>"A0001", "description"=>"Apple"},
{"linenumber"=>"5.1", "item"=>"A0002"},
{"linenumber"=>"6.1", "item"=>"Orange"}
]
}
Run Code Online (Sandbox Code Playgroud)
换句话说,我想在散列键中更改为小写.我知道我将不得不遍历哈希并使用downcase方法.如果有任何简单的方法在红宝石中这样做?
我正在尝试创建一个用于身份验证登录的mixins,因此它可以应用于我应该能够登录的模型.很像Ruby中的has_secure_password.
Afaik这是使用use基本需要模块的语句完成的,并调用__using__宏.所以我像这样实现了我的mixin.
defmodule MyApp.SecurePassword do
defmacro __using__(_options) do
quote do
import MyApp.SecurePassword
end
end
defmacro authenticate(password) do
# Lets return true, for testing purposes.
true
end
end
Run Code Online (Sandbox Code Playgroud)
然后我在我的"用户"模型中调用use.
defmodule MyApp.Farm do
use MyApp.Web, :model
use MyApp.SecurePassword
schema "farms" do
field :name, :string
field :email, :string
#.....
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我正在尝试使用该方法.
def create(conn, %{"session" => session_params}) do
user = Repo.get_by(Farm, email: session_params["email"])
if user && user.authenticate(session_params["password"]) do
conn = put_flash(conn, :success, "You were successfully logged in")
else
conn = put_flash(conn, …Run Code Online (Sandbox Code Playgroud) 我怎么能实现这个?我认为我的解决方案很脏,我想做得更好.我认为在Ruby中有一种简单的方法可以做到这一点,但我不记得了.我想在Rails中使用它,所以如果Rails提供类似的东西也可以.用法应该是这样的:
fruits = ['banana', 'strawberry', 'kiwi', 'orange', 'grapefruit', 'lemon', 'melon']
# odd_fruits should contain all elements with odd indices (index % 2 == 0)
odd_fruits = array_mod(fruits, :mod => 2, :offset => 0)
# even_fruits should contain all elements with even indices (index % 2 == 1)
even_fruits = array_mod(fruits, :mod => 2, :offset => 1)
puts odd_fruits
banana
kiwi
grapefruit
melon
puts even_fruits
strawberry
orange
lemon
Run Code Online (Sandbox Code Playgroud)
*******编辑*******
对于那些想知道的人,这就是我最终做到的:
在rails项目中,我创建了一个新文件config/initializers/columnize.rb,如下所示:
class Array
def columnize args = { :columns …Run Code Online (Sandbox Code Playgroud) 我想在method_missing抛出时为我的模型添加一些行为,但我知道searchlogic已经将它用于它们的命名搜索范围.
扩展method_missing功能的正确方法是什么?有没有办法对method_missing触发器做出反应?就像如何将函数绑定到javascript中的事件一样?
有没有办法在Ruby中循环枚举器?鉴于这段代码:
a=[1,2,3]
a.to_enum
a.next => 1
a.next => 2
a.next => 3
a.next => 1
Run Code Online (Sandbox Code Playgroud)
next当枚举数到达最后一个元素时,如何让方法返回第一个元素?