views:

176

answers:

5

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

+2  A: 

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.

mipadi
+1  A: 

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.

Pran
Meltemi
+1  A: 

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.

Daniel Vandersluis
A: 

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 manipulation 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!

Shyam
joining the #rubyonrails on freenode helps a lot, it surely did help me to understand Rails quicker.
Shyam
A: 

Technically, Symbols are a way of assigning a string to an integer id (which is sort of undercover)

example:

irb> :test
=> :test
irb> :test.to_s
=> "test"
irb> :test.object_id
=> -608128388
irb> :another.object_id
=> 166018
irb> :test == :another
=> false
irb> :test = :"test"
=> true
irb> "blah".to_sym
=> :blah

You might even think of them as enumerations but all of the enumerations are inside one enum block. This makes them great identifiers and as Pran suggested they are useful for hash keys.

I think everyone else covered strings and instance variables fairly well, so I will direct you to Shyam and Daniel for that.

DJTripleThreat