Hi, I’ve seen source like: params[:user] and params[‘user’], or also
:action => ‘new’ and :action => :new, is it also possible write
params[“user”]?
So, which are the differences between ', " and : ? when use for example
: instead of others?
I’ve read that, for example, in a put(‘name: #{user.name}’) the output
will be name: #{user.name} and with “” instead of ‘’ will be, for
example, name: John
is it right?
can anyone explain me these differences?
thanks
thanks
I’ve taken this: http://www.sapphiresteel.com/The-Little-Book-Of-Ruby
I’m going immediately to study it…
eddie wrote:
Hi, I’ve seen source like: params[:user] and params[‘user’], or also
:action => ‘new’ and :action => :new, is it also possible write
params[“user”]?
Please read a book about Ruby itself.
:yo is a symbol, ‘yo’ a string. A hash could contain :yo and ‘yo’ as
distinct keys. But :‘yo’ and ‘yo’.to_sym are the same as :yo.
Rails prefers symbols because they are mildly more physically
efficient, and they are much more cognitively efficient.
So, which are the differences between ', " and : ? when use for example
: instead of others?
I’ve read that, for example, in a put(‘name: #{user.name}’) the output
will be name: #{user.name} and with “” instead of ‘’ will be, for
example, name: John
is it right?
That’s because #{} calls .to_s, and :yo.to_s is ‘yo’.
“yo” is also a string, but “” markers can interpret advanced \ escapes
and #{} marks.
can anyone explain me these differences?
A book about Ruby may have come with your copy of it. Curl up with
that for a few hours, and your own code - between the Rails code -
will improve a lot.
–
Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!
Look on the Ruby mailing list, I asked the same question and got a great
answer. The Subject was “Symbols vs Strings”
I won’t reproduce it here since this is a Rails list and not a Ruby
list.
but check it out, and I think it will more than answer your question.
Matt
eddie wrote:
I’ve taken this: http://www.sapphiresteel.com/The-Little-Book-Of-Ruby
I’m going immediately to study it…
You might also like Why’s Poignant Guide to Ruby.
Why’s it poignant?
That’s the author’s pen-name; Why.
But why is his guide poignant.
Yes, I have heard Why’s guide is poignant like that.
But why is it…
Yep; it’s his book. Why.
–
Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!
eddie wrote:
thanks
I’ve taken this: http://www.sapphiresteel.com/The-Little-Book-Of-Ruby
I’m going immediately to study it…
That will not take you very far. The first edition of “Programming Ruby”
is available free, here:
along with a lot of other documentation.
Learn to use irb to try examples interactively.
regards
Justin
Eddie,
‘user’ != :user, but Rails uses HashWithIndifferentAccess instead of
standard Hash i many places, so you can use params[:user] or
params[‘user’]
See:
http://api.rubyonrails.org/classes/HashWithIndifferentAccess.html
http://redcorundum.blogspot.com/2006/07/symbolickeyhash.html
Alain
I’d have to add that there’s a lot of new things Ruby’s learned to do
between the two editions of the Pickaxe [the nickname readers have given
to
“Programming Ruby”]. Once you’ve made it through the first edition and
feel
like you and Ruby are a good fit you would do well to pick up a hard
copy of
the second edition. Everyone’s favorite method, Enumerable#inject, is a
prime example of what you’re missing out on.
RSL