Ruby : Anomalous Behavior of Ruby and Explanations

Ruby treats different objects differently. A novice Ruby programmer may wonder “what is happening in this house!” if he/she get stuck in such problems or Ruby behavior. You can see some of the behavior as

2.1.1 :041 > var1 = {name: 'john', age: 12}
 => {:name=>"john", :age=>12} 
2.1.1 :042 > var2 = var1
 => {:name=>"john", :age=>12} 
2.1.1 :043 > var2.object_id
 => 22850960 
2.1.1 :044 > var1.object_id
 => 22850960 
2.1.1 :045 > var1[:name]='Peter'
 => "Peter" 
2.1.1 :046 > var1
 => {:name=>"Peter", :age=>12} 
2.1.1 :047 > var2
 => {:name=>"Peter", :age=>12} 
2.1.1 :048 >
2.1.1 :048 > param = {id: 10, first_name: 'Bruce', last_name: 'Willis'} 
 => {:id=>10, :first_name=>"Bruce", :last_name=>"Willis"} 
2.1.1 :049 > def paramModifier(new_param)
2.1.1 :050?> new_param[:first_name] = 'Shiva'
2.1.1 :051?> end
 => :paramModifier 
2.1.1 :052 > paramModifier(param)
 => "Shiva" 
2.1.1 :053 > param
 => {:id=>10, :first_name=>"Shiva", :last_name=>"Willis"} 
2.1.1 :054 >
2.1.1 :054 > var1 = 10
 => 10 
2.1.1 :055 > var2 = var1
 => 10 
2.1.1 :056 > var1.object_id
 => 21 
2.1.1 :057 > var2.object_id
 => 21 
2.1.1 :058 > var1 = 100
 => 100 
2.1.1 :059 > var2
 => 10 
2.1.1 :060 > var1.object_id
 => 201 
2.1.1 :061 > var2.object_id
 => 21 
2.1.1 :062 >

2.1.1 :062 > var1 = 'PureString'
 => "PureString" 
2.1.1 :063 > var2 = var1
 => "PureString" 
2.1.1 :064 > var1.object_id
 => 21315620 
2.1.1 :065 > var2.object_id
 => 21315620 
2.1.1 :066 > var1 << ' Concatinated String'
 => "PureString Concatinated String" 
2.1.1 :067 > var2
 => "PureString Concatinated String" 
2.1.1 :068 > var1.object_id
 => 21315620 
2.1.1 :069 > var2.object_id
 => 21315620 
2.1.1 :070 >

Explanation .. Coming Soon

— Screenshots —

ruby_anomaly_1 ruby_anomaly_2 ruby_anomaly_3ruby_anomaly_4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s