简体   繁体   中英

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.

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

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.

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.

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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