小编Jim*_*Chu的帖子

基于Ruby语法的未定义局部变量

在以下Ruby代码中,

#! /usr/bin/env ruby
x = true
y = x and z = y
puts "z: #{z}"
Run Code Online (Sandbox Code Playgroud)

它将z: true按预期输出.

但在下面的一个中,我希望它具有相同的行为:

#! /usr/bin/env ruby
x = true
z = y if y = x
puts "z: #{z}"
Run Code Online (Sandbox Code Playgroud)

它导致了

未定义的局部变量或方法'y'表示main:Object(NameError)

这是为什么?

我理解我正在做一个赋值,并隐式检查赋值以确定是否运行z = y.我也明白,如果我添加y的声明y = nil,就x = 5在行之后,它将按预期传递并运行.

但是,期望语言if首先评估部分然后评估其内容,并且第二块代码与第一块代码的行为相同,这是不正确的?

ruby

7
推荐指数
1
解决办法
135
查看次数

从宏规则返回值!在锈

我正在通过示例了解Rust - 宏/DSL

代码显示:

macro_rules! calculate {
    (eval $e:expr) => {{
        {
            let val: usize = $e; // Force types to be integers
            println!("{} = {}", stringify!{$e}, val);
        }
    }};
}

fn main() {
    calculate! {
        eval 1 + 2 // hehehe `eval` is _not_ a Rust keyword!
    }

    calculate! {
        eval (1 + 2) * (3 / 4)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我希望我的自定义宏calculate返回计算值。我尝试了以下内容:

macro_rules! calculate {
  (eval $e:expr) => {{
    let val: usize = $e;
    println!("{} …
Run Code Online (Sandbox Code Playgroud)

rust

6
推荐指数
1
解决办法
2783
查看次数

如何在 Substrate 运行时开发中打印跟踪消息

在进行Parity Substrate运行时开发时,如何打印调试消息以跟踪和检查我的变量?

substrate

6
推荐指数
1
解决办法
1985
查看次数

Mandrill-api Excon :: Errors :: SocketError

我在Ruby中使用Mandrill-api以编程方式发送交易电子邮件.

我在rails应用程序中有(或多或少)以下行,

mandrill ||= Mandrill::API.new const(:API)[:MANDRILL_APIKEY]
... (constructing the message, content, etc)
mandrill.messages.send_template templ, template_content, message, true
Run Code Online (Sandbox Code Playgroud)

问题是在生产中运行时,偶尔会返回以下错误.

Excon::Errors::SocketError (EOFError (EOFError)):
app/mailers/mailer.rb:24:in `send'
....
Run Code Online (Sandbox Code Playgroud)

我不知道如何调试此问题.如果有人能让我了解调试方法,我非常感谢.

宝石信息:

  • mandrill-api(1.0.33)
  • excon(0.16.10)

生产环境:

 sudo bundle exec rake RAILS_ENV=production about


About your application's environment
Ruby version              1.9.3 (x86_64-linux)
RubyGems version          1.8.11
Rack version              1.4
Rails version             3.2.13
Active Record version     3.2.13
Action Pack version       3.2.13
Active Resource version   3.2.13
Action Mailer version     3.2.13
Active Support version    3.2.13
Middleware                Rack::Cache, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000001e72330>, Rack::Runtime, Rack::MethodOverride, …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails-3 mandrill excon

5
推荐指数
1
解决办法
951
查看次数

初始化()中的Ruby proc与lambda

我今天早上发现proc.new在类初始化方法中工作,但不是lambda.具体来说,我的意思是:

class TestClass

  attr_reader :proc, :lambda

  def initialize
    @proc = Proc.new {puts "Hello from Proc"}
    @lambda = lambda {puts "Hello from lambda"}
  end

end

c = TestClass.new
c.proc.call
c.lambda.call
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,结果将是:

Hello from Proc
test.rb:14:in `<main>': undefined method `call' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

这是为什么?

谢谢!

ruby lambda proc

4
推荐指数
1
解决办法
852
查看次数

支持 String 和 &amp;str 的 Hashmap

如何定义一个 HashMap 在其键和内容中都支持String&str?我尝试了以下方法:

fn mapping<T: Into<String>>() -> HashMap<T, T> {
  let mut map: HashMap<T, T> = HashMap::new();
  map.insert("first_name", "MyFirstName");
  map.insert("last_name".to_string(), "MyLastName".to_string());
  map
}

fn main() {
  let mut mapping = mapping();
}
Run Code Online (Sandbox Code Playgroud)

但它不编译,说:

error[E0599]: no method named `insert` found for type `std::collections::HashMap<T, T>` in the current scope
error[E0277]: the trait bound `T: std::cmp::Eq` is not satisfied
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
Run Code Online (Sandbox Code Playgroud)

copy-on-write rust borrowing run-time-polymorphism

3
推荐指数
1
解决办法
1876
查看次数

如何创建具有特定值的字节切片常量?

如何在 Rust 中创建字节切片常量,如下所示?

// This does not compile, but just to show my intention.
pub const MyConst: &'static [u8; 256] = b"abcdef" + [0u8; 250];

// Basically, the value is b"abcdef00000...", the `000...` appended at the end 
// are a series of byte 0, not character 0.
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
2
解决办法
2906
查看次数

decl_storage 中`pub` 的目的是什么?

在基板中实现运行时模块时,给定以下存储

decl_storage! {
  trait Store for Module<T: Trait> as CatAuction {
    Kitties get(kitties): map T::Hash => Kitty<T::Hash, T::Balance>;
    KittyOwner get(owner_of): map T::Hash => Option<T::AccountId>;
    OwnedKitties get(kitties_owned): map T::AccountId => T::Hash;

    pub AllKittiesCount get(all_kitties_cnt): u64;
    Nonce: u64;

    // if you want to initialize value in storage, use genesis block
  }
}
Run Code Online (Sandbox Code Playgroud)

pub前面的目的是什么AllKittiesCount?因为不管有pub没有,polkadot UI 还是可以查询到的,就好像它是一个公共变量一样。

parity-io substrate

2
推荐指数
1
解决办法
240
查看次数

基底存储中私有变量的可能性

是否可以将私有变量存储在底层存储中,特别是以以下形式存储并在私有函数中访问它们?

#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct MyStruct {
  id: Hash, // the `id` is public 
  // 1. Can this be a private variable not returned by API / to client?
  private_var: u64, 
}

decl_storage! {
  trait Store for Module<T: Trait> as MyModule {
    // 2. Can this be private storage used only within module function implementation, but cannot be called by API/client?
    PrivateStorage: u64 = 0; 
    PublicStruct: MyStruct;
  }
}

decl_module! { }

impl<T: Trait> Module<T> { …
Run Code Online (Sandbox Code Playgroud)

substrate

2
推荐指数
1
解决办法
299
查看次数