1

I am just beginning to learn Ruby and have the following code in a project:

USER_ATTRS = [:login, :password, :lastname, :firstname, :mail, :admin] 

What does this do? Where do :login and all the other things come from?

Are they just properties of a class (user, probably)?

Can someone can help me out please? Because I can't really find this on the net.

1

3 Answers 3

3

The short answer is:

"The ':' indicates a Ruby symbol'


The medium length answer would add:

Symbols are immutable objects that differ from strings in important ways.


The Real Answer:
You will have to learn Ruby to learn Rails. I also labored under the impression that I could just pick-up Rails and leave Ruby as just one more language under my belt. I was wrong. Ruby is different. I could point out how it is interpreted and makes use of open classes, but this isn't the real reason you too will have trouble "learning as you do' with Ruby and Rails. Ruby allows for a kind of syntactical overloading that some call 'syntactic sugar' It is one way to make Ruby code shorter and more readable. It will also hinder your ability to simply infer functionality from the code you see. You will see many well conceived Rails examples that leave you puzzled and eventually frustrated. These examples will look terse and mysterious and you may wonder, "Why don't they just make it more intuitive?" The answer is many will also look at those same examples and say, "That's good Ruby." They have their reasons.

The Remedy:
Read. http://www.manning.com/black2/ and and http://pragprog.com/book/ppmetr/metaprogramming-ruby This rails book http://pragprog.com/book/rails4/agile-web-development-with-rails and these guides http://guides.rubyonrails.org/ then took me where I needed to go.

What makes this point best? If you haven't yet, work through this outstanding tutorial http://ruby.railstutorial.org/ruby-on-rails-tutorial-book It is one of the best programming tutorials I have ever read. It covers Rails very well and says some things about Ruby. Yet, it fails. If it is the only source you work from, you will not know enough Ruby to progress to the point where you can generate your own code.

Sign up to request clarification or add additional context in comments.

7 Comments

He hasn't mentioned Rails anywhere. What makes you assume he wants to learn Rails?
Mischa, you are right. I did assume Rails here. Certainly there are other possibilities to consider. If the discussion heads in a non-rails direction, I'll delete my answer. Thanks - Perry
:-) Check out the original (before the edits) version. I guess my answer stays. I suppose I would have left some of those edits in the original question.
You are right, he was talking about learning Rails in his original question. I would delete my original comment, but then your comments don't make sense anymore ;-)
I think You're right, I just have to learn Ruby all the way. Because i want to implement something but i'm not really capable for doing this. Thanks anyway!
|
2

They are called symbols. You can find the API reference for symbols here. And this link may be helpful too. As Slawosz says, from this one line of code it's hard to say where it comes from and what it does.

1 Comment

...and are part of the USER_ATTRS array.
1

USER_ATTRS is a constant which contains array of symbols - you can think about symbols as immutable strings. It is hard to say why it is in your program, please insert more source for better anwser. But for me, as name says this symbols denotes user attributes, stored in one place for make program cleaner.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.