1

I understand that symbols and strings are not same, but I don't understand the concept of symbols.

As I understand, a symbol refers to something else. I use the word refer here for my better understanding, not in the in sense of C/C++ pointers or references.

  1. When I create a new controller in Rails, using something like rails g controller Posts, does Ruby automatically create a new symbol :posts, which refers to the Posts controller? Is this :posts symbol globally accessed from anywhere in the application? Only in that case, I can understand a construct like:

    resources :posts
    

which Ruby can translate for itself: "Ok, I know that :posts refers to posts_controller, so I need to create CRUD routes for that controller". Am I right?

  1. What roles do symbols :title and :content have in this example? I assume that form.label is equivalent to form.label(:title).

    <%= form_for @post, :html => {:class => "new_post"} do |form| %>
    <%= form.label :title %><br>
    <%= form.text_field :title %><br>
    
    <%= form.label :content %><br>
    <%= form.text_area :content %><br>
    
    <%= form.submit("Add new post") %>
    
  2. Are there built-in symbols like :all and symbols that are created on-the-way? When I say on-the-way, as I can see when I create some method like:

    def something
        ...
    end
    

    Does Ruby create a new symbol :something, which will refer to method something? If that is correct, why do I need that symbol, where can I can use it?

    @posts = Post.all #Or Post.all() => Standard OOP approach, i know what happens here
    @posts1 = Post.find(:all) # Dont know what happens here
    

    What about @posts1, what am I doing here? What do :all mean here? Is it some constant value (not string) for Ruby to know that it must pull every Post from DB?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Srle
  • 10,366
  • 8
  • 34
  • 63
  • See also: http://stackoverflow.com/a/11488310/1301972 – Todd A. Jacobs Oct 18 '13 at 14:59
  • You're really confusing Ruby, the language, and what Rails does by taking advantage of Ruby's features. `:symbols` are part of Ruby, and are a core feature for memory-savings and speed-ups. Rails generates symbols for those reasons. I'd highly recommend backing away from Rails for a while, and concentrate on Ruby until you learn the "magic" of Ruby's introspection and dynamic creation of classes, variables and symbols. Once you understand Ruby better you'll find that Rails will start to make a LOT more sense, because it takes great advantage of that magic. – the Tin Man Oct 18 '13 at 16:23

5 Answers5

2

A Symbol in Ruby is just an immutable string with a special syntax. It's used mostly so you can use less memory when doing string matching like operations as in:

options = { :me => 'joe' }
puts options[:me]

This creates a single symbol for :me, if you used strings:

options = { 'me' => 'joe' }
puts options['me']

Two strings would have been created, since strings in Ruby are mutable. So, no, there's no special meaning and it's not something that refer on something other, it's mostly a hack to the fact that strings in Ruby are mutable by default.

As for your last question, :all is just a value that the method implements in something like "a == :all ? return everything : so something else". It's just a method parameter.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
2

Symbol is just an immutable interned string meaning that all symbol variables with the same value point to exactly the same memory location. They also never garbage collected so avoid creating them with to_sym. They are frequently used as keys in hash tables but here's the catch - some hash tables you'll receive from external libraries (e.g. API clients) have strings as keys so when you try to get the value as you're used with with obj[:some_attribute] you'll get nil.

synapse
  • 5,588
  • 6
  • 35
  • 65
0

:all is instance of Symbol class, it's built-in Ruby class. All

@posts1 = Post.find(:all)

line is deprecated way to load all posts to @posts1 variable. :all symbol is what indicates that you want to fetch all posts. If you type:

@post = Post.find(5)

you'd get only one post, the one with id = 5.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
0

symbol is the same as the string, but one with the same value have the same memory address. And this is a key difference.

puts 'string'.object_id
puts 'string'.object_id
puts 'string'.object_id

#=> 3099310
#=> 3099310
#=> 3099310

puts :symbol.object_id
puts :symbol.object_id
puts :symbol.object_id

#=> 3098410
#=> 3021341
#=> 3012440

next

 Post.find :all

Same as

Post.find 'all'

:posts isn't reference to posts_controller

resources :posts

What does it mean? resources is a method who generate standart CRUD routes. For search controller, it use the naming convention. If resource named as :posts, then routes will be set to PostsController.

MaxKonin
  • 468
  • 3
  • 16
0

I will recommend you to have a look to this blog Ruby Symbol and String.

You will get a lot more apart from symbol and string definition with details example.

Pravin Mishra
  • 8,298
  • 4
  • 36
  • 49