简体   繁体   English

Ruby on Rails- :symbols、@iVars 和“字符串”-天哪!

[英]Ruby on Rails- :symbols, @iVars and “strings” - oh my!

New to Rails and trying to get my head around when/why to use :symbols , @ivars , "strings" within the framework. @ivars Rails 并试图弄清楚何时/为什么在框架内使用:symbols@ivars"strings"

I think I understand the differences between them conceptually我想我从概念上理解它们之间的差异

  • only one :symbol instance per project每个项目只有一个:symbol实例
  • one @ivar per instance每个实例一个@ivar
  • multiple "strings" - as they are created whenever referenced (?)多个"strings" - 因为它们是在引用时创建的(?)

Feel free to correct me!随时纠正我!

The main confusion comes from understanding the rules & conventions of what Rails expects - where and WHY?主要的困惑来自理解 Rails 期望的规则约定——在哪里以及为什么?

I'm sure there's an "Ah ha!"我敢肯定有一个“啊哈!” moment coming but I haven't had it yet...as it seems pretty arbitrary to me (coming from C/Obj-C).那一刻即将到来,但我还没有……因为这对我来说似乎很随意(来自 C/Obj-C)。

-thx -谢谢

The @instance_variable is an instance variable. @instance_variable是一个实例变量。 It is usually defined in the controller and accessible in the views.它通常在控制器中定义并在视图中访问。

The "string" is a string, like as in any other language. "string"是一个字符串,就像在任何其他语言中一样。

The :symbol , is as you mentioned it's an efficient way of representing names and strings; :symbol ,正如你所提到的,它是一种表示名称和字符串的有效方式; they are literal values.它们是字面值。 It is initialized and exists only once during the ruby session.它被初始化并且在 ruby​​ 会话期间只存在一次。 It's not a string, since you don't have access to String methods;它不是字符串,因为您无权访问 String 方法; it's a Symbol.这是一个符号。 On top of that, it's immutable.最重要的是,它是不可变的。 For those reasons, it becomes very handy in representing keys in hashs.出于这些原因,在散列中表示键变得非常方便。 Rails methods uses hashes, thus, you find symbols a bit everywhere in Rails. Rails 方法使用散列,因此,您在 Rails 中到处都可以找到符号。

Instance variables are pretty straightforward: they track properties/values of a particular instance, so you use them when you the values will vary across instances.实例变量非常简单:它们跟踪特定实例的属性/值,因此当您的值因实例而异时,您可以使用它们。

Symbols vs. strings are a bit more arbitrary.符号与字符串比较随意。 Symbols are generally used for constant values, in much the same way that a language such as C would use enums;符号通常用于量值,与 C 等语言使用枚举的方式非常相似; Ruby doesn't have enums, so symbols are often used to fill that gap. Ruby 没有枚举,因此通常使用符号来填补这一空白。 Strings are used for more varied pieces of text that won't be used as a flag or similar constant.字符串用于更多样化的文本片段,不会用作标志或类似的常量。

Symbols are kind of like pointers (not in the C-ish way, but in C-ish thinking, they point).符号有点像指针(不是在 C 语言中,而是在 C 语言思维中,它们指向)。 Well, you use symbols when you are manipulating properties.好吧,您在操作属性时会使用符号。 They are one of the great benefits of dynamic typing if you'd ask me.如果您问我,它们是动态类型的一大好处。 (For potential voters I do not mean any harm, I do know that they are not pointers, but it felt 'ah-ha!' for me). (对于潜在的选民,我没有任何伤害的意思,我知道它们不是指针,但对我来说感觉“啊哈!”)。

:action => "index"

Instance variables are needed when you fetch data from your model and you want to use them across your views (inside your controller method).当您从模型中获取数据并希望在您的视图中(在您的控制器方法内)使用它们时,需要实例变量。

def my_controller_method
@myposts = Post.find(:all)
end

# inside view
<% for @myposts do |m| %>
<i><%= m.title %></i>
<% end %>

Just a heads up, the rules and conventions kinda change rapidly (as I discovered on my Rails journey) quite a lot per version.提醒一下,每个版本的规则和约定变化很快(正如我在 Rails 之旅中发现的那样)。 Having the right guide with the right Rails helps.拥有正确的指南和正确的 Rails 会有所帮助。 Good luck with coding!祝你编码好运!

Instance variables don't really belong in the same list as strings and symbols.实例变量并不真正与字符串和符号属于同一个列表。 Strings and Symbols are types of classes whereas instance variables are a type of variable .字符串和符号是类的类型,而实例变量是变量的类型 So instance variables ( @var ) are just a way to store a value between methods of one instance of one class:所以实例变量( @var )只是一种在一个类的一个实例的方法之间存储值的方法:

class Calculator
  @counter = 0

  def inc
    @counter += 1
  end

  def dec
    @counter -= 1
  end
end

Here is a good article on the distinction between symbols and strings. 是一篇关于符号和字符串区别的好文章。

Rails 控制器通过 ORM(对象关系映射)通过 Models 访问 rails 数据库,即 Model 类将映射到其对应的表,Objects 直接映射到表中的行。为了获取给定用户查询的结果,实例变量 (@instance_variable) 是处理它的完美选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM