具有类型和转换的结构
我试图在 Ruby 中完成以下任务:
person_struct = StructWithType.new "Person",
:name => String,
:age => Fixnum,
:money_into_bank_account => Float
我希望它接受两者:
person_struct.new "Some Name",10,100000.0
也就是说
person_struct.new "Some Name","10","100000.0"
,我希望它自动执行数据转换。
我知道 Ruby 是动态的,我不应该关心数据类型,但这种转换会很方便。
我要问的是类似于 ActiveRecord 已经做的事情:将 String 转换为表列中定义的数据类型。
在搜索 ActiveModel 之后,我不知道如何使用一些 TableLess 来执行此转换。
毕竟我认为我的问题可能需要比 ActiveModel 模块提供的少得多。
当然,我可以自己实现一个提供此转换功能的类,但我宁愿知道这还没有完成,以免重新发明轮子。
提前谢谢。
I am trying to accomplish the following in Ruby:
person_struct = StructWithType.new "Person",
:name => String,
:age => Fixnum,
:money_into_bank_account => Float
And I would like it to accept both:
person_struct.new "Some Name",10,100000.0
and
person_struct.new "Some Name","10","100000.0"
That is, I'd like it to do data conversion stuff automatically.
I know Ruby is dinamically and I should not care about data types but this kind of conversion would be handy.
What I am asking is something similar to ActiveRecord already does: convert String to thedatatype defined in the table column.
After searching into ActiveModel I could not figure out how to to some TableLess that do this conversion.
After all I think my problem may require much less that would be offered by ActiveModel modules.
Of course I could implement a class by myself that presents this conversion feature, but I would rather know this has not yet been done in order to not reinvent the wheel.
Tks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为类内部的实现是如此简单,而且根本没有任何开销,所以我根本不明白使用
StructWithType
的原因。 Ruby 不仅是动态的,而且在存储其实例方面非常高效。只要不使用属性,就没有属性。类中的实现应该是:
StructWithType
中的实现将更高一层:StructWithType
实例(而非类)的new
实现中使用类的转换器来进行转换。它的第一个草图可能是这样的:
这里的想法是:
argname[]
)。因此,您必须实现结构体的创建、转换器的查找、参数名称的查找以及新实例属性的设置器。抱歉,今天没时间了...
我使用了
create
,因为new
在Ruby中具有不同的含义,我不想把它搞乱。I think that the implementation inside a class is so easy, and there is no overhead at all, so I don't see the reason to use
StructWithType
at all. Ruby is not only dynamic, but very efficient in storing its instances. As long as you don't use an attribute, there is none.The implementation in a class should be:
The implementation in
StructWithType
would then be one layer higher:new
implementation ofStructWithType
instances (not class) the converters of the class to do the conversion.A very first sketch of it could go like that:
The ideas here are:
argname[]
has to be written) the new value.So you have to implement the creation of the struct, the lookup for converter, the lookup for the argument name and the setter for the attributes of the new instance. Sorry, no more time today ...
I have used
create
becausenew
has a different meaning in Ruby, I did not want to mess this up.我在github上找到了一个可以满足我的一些要求的项目:ActiveHash。
尽管我仍然必须为每种类型创建一个类,但类型转换是免费的。
我正在尝试。
使用示例:
I have found a project in github that fulfill some of my requirements: ActiveHash.
Even though I still have to create a class for each type but the type conversion is free.
I am giving it a try.
Usage example: