-1

im working through a Rails udemy course and were touching on the part of just ruby right now (Which I have NO experience with so this is all new to me).

In this portion were basically taking "permissions" from a permissions file and putting it into a "user json".

require 'json'

class User


  attr_accessor :email, :name, :permissions 

  def initialize(*args)
    @email = args[0]
    @name = args[1]
    @permissions = User.permissions_from_template

  end

  def self.permissions_from_template
    file = File.read 'user_permissions_template.json'
    JSON.load(file, nil, symbolize_names: true)
  end

  def save
   self_json = {email: @email, name: @name, permissions: @permissions}.to_json
   open('users.json', 'a') do |file|
     file.puts self_json
   end
  end
end

There is a simple runner.rb file which creates the user and initializes it, thats not my question though. Basically my question is the self_json = {email: @email, name: @name, permissions: @permissions}.to_json

Ive read the to_json API and it's simple enough, but every example of a ruby object i've seen looks like this: {:email => @email, :name => @name, :permissions => @permissions} except replace the @ symbols with "strings"

Im getting confused between the variable types of @variable and :variable.

Are we not using :var => @whatever since were using @ variables or?

msmith1114
  • 2,717
  • 3
  • 33
  • 84

1 Answers1

1

It has nothing to do with values being taken from instance variables instead of strings; all it has to do with is keys being symbols.

Old Ruby had only one object literal syntax, with each key/value pair

key => value

e.g.

{ "foo" => "bar", 2 => 4, :name => "value" }

where "foo" is a string key, 2 an integer key, and :name a symbol key.

However, it was found that symbol keys were used very, very often, especially as an alternative to (then missing from language) keyword parameters to functions:

link_to "Delete", thing, :method => :delete, :data => { :confirm => "Sure?" }

which is actually a shortcut for

link_to("Delete", thing, { :method => :delete, :data => { :confirm => "Sure?" } })

To make this kind of construction less verbose, and more intuitive as using keywords, a new syntax was developed as a shortcut, specifically for keys that are symbols, where

key: value

is an alternative to the older but identical

:key => value

So the above hash could have been written as

{ "foo" => "bar", 2 => 4, name: "value" }

(although I don't recommend it in this case, because mixing the two styles is kind of icky.)

Using this new syntax, the above command could be written so:

link_to "Delete", thing, method: :delete, data: { confirm: "Sure?" }

Thus, there is absolutely no difference if you write

{ :email => @email, :name => @name, :permissions => @permissions }

or

{ email: @email, name: @name, permissions: @permissions }

Currently, the latter one is preferred, as it is shorter and more readable. However, in many cases if the code has been around since pre-Ruby-1.9, one can still find the old syntax floating around.

Amadan
  • 191,408
  • 23
  • 240
  • 301