小编use*_*302的帖子

如何从整数转换为字符串?

我无法编译将类型从整数转换为字符串的代码.我正在运行Rust for Rubyists教程中的一个示例,该教程具有各种类型转换,例如:

"Fizz".to_str()num.to_str()(其中num是整数).

我认为这些to_str()函数调用的大部分(如果不是全部)都已被弃用.将整数转换为字符串的当前方法是什么?

我得到的错误是:

error: type `&'static str` does not implement any method in scope named `to_str`
error: type `int` does not implement any method in scope named `to_str`
Run Code Online (Sandbox Code Playgroud)

string int type-conversion rust

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

Ruby通过自定义方法查找?

我试图找出一种方法,通过将ActiveRecord模型与常规方法混合使用的常规rails 3.0语法进行搜索。

例如:

class User < ActiveRecord::Base
 def custom_method
   if [...]
     return true
   else 
     return false
   end     
 end
end
Run Code Online (Sandbox Code Playgroud)

并且我想以这种方式使用ActiveRecord来激活它:

User.find(:all).where(custom_method is true)
Run Code Online (Sandbox Code Playgroud)

有什么办法吗?抱歉,如果语法与我要传达的内容不正确。

编辑:我想澄清一下,custom_method使用起来是相当复杂的,因此最好将其调用而不是将其转换为sql语法。

ruby methods activerecord ruby-on-rails find

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

ruby DRY返回值?

这可能是一个愚蠢的问题,但我的rails应用程序开始受到这种代码风格的困扰:

def return_my_object
  @object = get_object
  some_function_work(@object)
  return @object
end
Run Code Online (Sandbox Code Playgroud)

是否有一般做法或方法来摆脱或避免return @object代码?到目前为止,我无法想到任何东西,它似乎是残酷的代码.

ruby return ruby-on-rails dry

0
推荐指数
1
解决办法
66
查看次数

标签 统计

ruby ×2

ruby-on-rails ×2

activerecord ×1

dry ×1

find ×1

int ×1

methods ×1

return ×1

rust ×1

string ×1

type-conversion ×1