I am confused by the two different syntaxes. Both work just fine and
there is not a difference in the outcome, so which? And why two
methods? What’s the bigger picture?
Jeremy
I am confused by the two different syntaxes. Both work just fine and
there is not a difference in the outcome, so which? And why two
methods? What’s the bigger picture?
Jeremy
On 31-Aug-06, at 10:44 PM, Jeremy C. wrote:
I am confused by the two different syntaxes. Both work just fine and
there is not a difference in the outcome, so which? And why two
methods? What’s the bigger picture?
The arguments are converted to symbols (whether they are strings, or
symbols to begin with), so it doesn’t matter. I favour the symbol
version because it looks prettier.
irb(main):003:0> :table_name.to_sym == “table_name”.to_sym
=> true
/Jeff
the bigger picture is symbols vs strings.
the quick answer:
symbols and strings are pretty much the same for the most part. the
big difference is at the memory level. every time you use a string,
no matter if it’s been used before, memory is allocated as it’s a new
instance. not so with symbols.
ex:
irb(main):003:0> a = :foo
=> :foo
irb(main):004:0> b = :foo
=> :foo
irb(main):005:0> a.object_id
=> 3756302
irb(main):006:0> b.object_id
=> 3756302
irb(main):007:0> x = “foo”
=> “foo”
irb(main):008:0> y = “foo”
=> “foo”
irb(main):009:0> x.object_id
=> -604402436
irb(main):010:0> y.object_id
=> -604412996
notice that the object_ids for a and b are the same, but for x and y
they are different.
so if you use the same string over and over, especially in parameters
passed in Rails methods, you’re going to be consuming more memory than
if you used symbols.
for more detailed explanations, google for ‘ruby symbols’
Chris
symbols and strings are pretty much the same for the most part. the
big difference is at the memory level. every time you use a string,
no matter if it’s been used before, memory is allocated as it’s a new
instance. not so with symbols.
None of this really matters for a migration. I tend to use strings
because thats what my textmate bundle uses. Though I should change
it, symbols turn a really neat blue color.
–
Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs