English 中文(简体)
当使用标志代替鲁比的变量时?
原标题:When is it correct to use symbols in place of variables in Ruby?

I m new to Ruby and trying to get my head around some of its syntax.

为什么这一法典在例外条款中有所改动:

begin
  puts Dir::delete
rescue ArgumentError => e
  puts e.backtrace
end

但没有文号?

begin
  puts Dir::delete
rescue ArgumentError => :e
  puts e.backtrace
end
最佳回答

联合国文件都用英文大写字母附加数字编号。 在你的例子中,你需要一个变数来储存Error物体。 你们通常使用符号作为固定不变物。

例如,如果你创设一个带有卡片指示的模块,则最好使用符号<代码>:north,:South,:east ,: 西。 http://www.un.org/Depts/DGACM/index_chinese.htm

Symbols are often used as keys in hashes:

my_hash = { a: 1, b: 7, e: 115 }

例如,阅读关于烟雾的废墟代码非常有用,以便了解何时使用符号。

问题回答

Because, as you write in the question itself, you need an Exception object, not a Symbol object.

In the rescue block you re accessing backtrace via the e object, which is of type ArgumentException, not of the type Symbol.

因此,当口译栏目<代码>:e间接生成新的<代码>Symbol物体并设定其价值为:e时,实际发生的情况如何。 类似文件23,其中“Fixnum物体被间接设定,其价值为23。

但文号本身可以储存在一个变量中:

some_var = :e
e = :e

我希望它能明确我的含义。

联合国文件都用英文大写字母附加数字编号。 Apple果和range;

a. 替代符号的变数——如您所希望(如<代码>=:名称;

代替方言——谨慎(如果你创造太多的象征,你可能会陷入麻烦)。

我认为,<代码>e是一种可变的变量,可储存例外物体和<代码>:e为数据类型,因此具有价值。

一些实例

# standard way of assign variable will work
e = ArgumentError.new 

# assign to data will not work
:e = ArgumentError.new
 e  = ArgumentError.new
 1 = ArgumentError.new




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签