Ruby: what's the difference between initializing a string using quotes vs colon? -
whats' difference between initializing string using quotes vs preceding colon? i.e "bobo" vs :bobo. when inspect them appear same when compare them result evaluates false.
irb(main):006:0> r = "bobo" => "bobo" irb(main):007:0> puts r bobo => nil irb(main):008:0> t = :bobo => :bobo irb(main):009:0> puts t bobo => nil irb(main):010:0> puts r == t false => nil irb(main):011:0> s = "bobo" => "bobo" irb(main):012:0> puts r == s true => nil
"bobo" string whereas :bobo symbol.
:bobo.class # => symbol "bobo".class # => string
if obj not string, returns false. otherwise, returns true if str <=> obj returns zero.
so according documentation "bobo" == :bobo # => false
, "bobo" == "bobo" # => true
. - expected.
puts :bobo # >> bobo :bobo.to_s # => "bobo"
this because puts
applying to_s
on symbol object :bobo
. see symbol#to_s
Comments
Post a Comment