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

发布于 2024-09-04 08:28:05 字数 467 浏览 10 评论 0原文

Rails 新手,试图了解何时/为何在框架内使用 :symbols@ivars"strings"

我想我从概念上理解它们之间的差异,

  • 每个项目只有一个 :symbol 实例,
  • 每个实例有一个 @ivar
  • 多个 "string" - 因为它们是每当引用时都会创建(?)

请随时纠正我!

主要的困惑来自于对规则和规则的理解。 Rails 期望的约定 - 地点和原因?

我确信有一个“啊哈!”时刻即将到来,但我还没有拥有它......因为它对我来说似乎相当随意(来自 C/Obj-C)。

-谢谢

New to Rails and trying to get my head around when/why to use :symbols, @ivars , "strings" within the framework.

I think I understand the differences between them conceptually

  • only one :symbol instance per project
  • one @ivar per instance
  • multiple "strings" - as they are created whenever referenced (?)

Feel free to correct me!

The main confusion comes from understanding the rules & conventions of what Rails expects - where and WHY?

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).

-thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江南烟雨〆相思醉 2024-09-11 08:28:05

@instance_variable 是一个实例变量。它通常在控制器中定义并在视图中访问。

"string" 是一个字符串,就像任何其他语言一样。

:symbol 正如您所提到的,它是表示名称和字符串的有效方式;它们是字面值。它在 ruby​​ 会话期间被初始化并仅存在一次。它不是字符串,因为您无权访问 String 方法;这是一个符号。最重要的是,它是不可变的。由于这些原因,用散列表示键变得非常方便。 Rails 方法使用哈希,因此,您会在 Rails 中随处发现符号。

The @instance_variable is an 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.

The :symbol, is as you mentioned it's an efficient way of representing names and strings; they are literal values. It is initialized and exists only once during the ruby session. It's not a string, since you don't have access to String methods; 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.

活雷疯 2024-09-11 08:28:05

实例变量非常简单:它们跟踪特定实例的属性/值,因此当值因实例而异时可以使用它们。

符号与字符串更加任意。符号通常用于常量值,与 C 等语言使用枚举的方式非常相似; Ruby 没有枚举,因此通常使用符号来填补这一空白。字符串用于更多样化的文本片段,这些文本片段不会用作标志或类似的常量。

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; Ruby doesn't have enums, so symbols are often used to fill that gap. Strings are used for more varied pieces of text that won't be used as a flag or similar constant.

聚集的泪 2024-09-11 08:28:05

符号有点像指针(不是以 C 风格的方式,而是以 C 风格的思维,它们指向)。嗯,当你操作属性时,你会使用符号。如果你问我的话,它们是动态类型的巨大好处之一。 (对于潜在选民,我无意伤害,我确实知道他们不是指针,但对我来说感觉“啊哈!”)。

:action => "index"

当您从模型中获取数据并且想要在视图中(在控制器方法内)使用它们时,需要实例变量。

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

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

请注意,每个版本的规则和约定变化很快(正如我在 Rails 之旅中发现的那样)。拥有正确的导轨和正确的 Rails 会有所帮助。祝你编码顺利!

Symbols are kind of like pointers (not in the C-ish way, but in C-ish thinking, they point). 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. Having the right guide with the right Rails helps. Good luck with coding!

送你一个梦 2024-09-11 08:28:05

实例变量实际上并不与字符串和符号属于同一列表。字符串和符号是类的类型,而实例变量是变量的类型。因此实例变量(@var)只是在一个类的一个实例的方法之间存储值的一种方式:

class Calculator
  @counter = 0

  def inc
    @counter += 1
  end

  def dec
    @counter -= 1
  end
end

这里是一篇关于符号和字符串之间区别的好文章。

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:

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.

奈何桥上唱咆哮 2024-09-11 08:28:05

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

The Rails controller access the rails database through Models by ORM (Object Relation Mapping)i.e Model class will mapped to its corresponding table and Objects are directly mapped to rows in the table.In order to get the results for a given user query,the instance variable (@instance_variable) is the perfect choice to deal with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文