Srle
Srle

Reputation: 10496

Am I doing symbols right?

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?

Upvotes: 1

Views: 134

Answers (5)

synapse
synapse

Reputation: 5728

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.

Upvotes: 2

Pravin Mishra
Pravin Mishra

Reputation: 8444

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.

Upvotes: 0

MaxKonin
MaxKonin

Reputation: 468

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.

Upvotes: 0

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40333

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.

Upvotes: 2

Marek Lipka
Marek Lipka

Reputation: 51191

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

Upvotes: 0

Related Questions