本文介绍了Ruby相当于C#'using'语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进入红宝石在过去数个月,但我还没有想出但有一点是红宝石相当于C#的(和其他语言)是什么使用语句。



我一直在使用要求语句来声明对宝石我的依赖关系,但我我越来越懒惰,宁愿不完全与他们的模块(空间)的名字限定我经常使用的类名。



当然,这是可能的,对不对?如谷歌也没有给我任何有用的东西我一定不能使用正确的术语。


解决方案

 >>数学:: PI 
=> 3.14159265358979
>> PI
NameError:从(IRB)未初始化的常数PI
:;> 3
>包括数学
=>目标
>> PI
=> 3.14159265358979



OTOH,如果问题只是走样类的名称,认为,正如他们所说的类是一个对象,而对象是类



这样:

 >>需要'CSV'
>> R = CSV ::读卡器
>> r.parse什么,永远做| E | PE结束
[是什么,永远]



是的,在Ruby中的类名字只是像任何其他类的对象的引用。


I've been getting into Ruby over the past few months, but one thing that I haven't figured out yet is what the Ruby equivalent of C#'s (and other languages) using statement is.

I have been using the require statement to declare my dependencies on Gems, but I am getting lazy and would prefer to not fully qualify my frequently used class names with their module (namespace) name.

Surely this is possible, right? I must not be using the right terminology as Google hasn't given me anything useful.

解决方案
>> Math::PI
=> 3.14159265358979
>> PI
NameError: uninitialized constant PI
    from (irb):3
>> include Math
=> Object
>> PI
=> 3.14159265358979

OTOH, if the issue is just aliasing class names, consider that, as they say "Class is an object, and Object is a class".

So:

>> require 'csv'
>> r = CSV::Reader
>> r.parse 'what,ever' do |e| p e end
["what", "ever"]

Yes, in Ruby the class name is just a reference like any other to an object of class Class.

这篇关于Ruby相当于C#'using'语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:51