Hello, ruby beginners. Hope you have read ruby basics. let's revise few things.

**1)Arithmatics**

5/2=2

2/4=0

5/2.0=2.5

"Kiprosh"+123 #<TypeError: can't convert Fixnum into String> as 123 is Fixnum and "Kiprosh" is a string.

"Kiprosh"+123.to_s => Kiprosh123

**2)Hash:**

**a)**

consider the following hash:

h={"abcd"=>"pqrs","abc"=>"xyz" }

How to access.?

h["abcd"] will return "pqrs"

similarly h["abc"] will return xyz

we can store keys in variables and can access the values like following

x="abcd" then h[x]=>"pqrs"

x="abc" then h[x]=>"xyz"

**b)**

h={a:"abc",b:"pqr",c:"xyz"} this hash defination is similar to h={:a=>"abc",:b=>"pqr",:c=>"xyz"}

and in both cases to access the values we need to write like h[:a]

h[:a]=>"abc"

h[:b]=>"pqr"

h[:c]=>"xyz"

if we want to use variables x for accessing values then,

x=:a

h[x]=>"abc"

x=:b

h[x]=>"pqr"

x=:c

h[x]=>"xyz"

**c)**

consider following nested hash,

h={a:{b:{c:{d:"abc"}}}}

here value of key a is : {b:{c:{d:"abc"}}} hash

in that value of key b is : {c:{d:"abc"}} hash

in that value of key c is :{d:"abc"} hash

and the value of key d is a string "abc" .

once we undersood this structure of nested hash we can access various hash elements as follow:

h[:a] => {:b=>{:c=>{:d=>"abc"}}}

h[:a][:b] => {:c=>{:d=>"abc"}}

h[:a][:b][:c]=> {:d=>"abc"}

h[:a][:b][:c][:d] => "abc"

**d)**

consider following...

h=[{a:[{b:"abc",c:"pqr"}],b:{c:{1=>2}}},2]

what is this? is it invalid ?

friends, here h is an array of 2 elements where,

h[0] => {:a=>[{:b=>"abc", :c=>"pqr"}], :b=>{:c=>{1=>2}}}

h[1] => 2

where first element of array h is hash which is again nested. lets access "abc".

h[0][:a][0][:b] => "abc"

explanation,

here, first, h[0] will return {:a=>[{:b=>"abc", :c=>"pqr"}], :b=>{:c=>{1=>2}}}

then [:a will return an array of one element and that one element is hash {:b=>"abc", :c=>"pqr"}

then [:b] will return "abc". Now, lets access few more elements.

h[0][:b][:c][1] => 2

h[0][:a][0][:c] => "pqr"

h[0].keys => [:a, :b] # it tells h[0] contains hash with 2 keys

h[0][:a][0].keys => [:b, :c]

**3)**

In the case of inheritance, the same copy of the class variable is used by both base class and derived class so it's preferable to use class instance variables in that case. consider this example:

class Abc @@a=1 def self.xyz puts "value of a in Abc a=#{@@a}" end end class Pqr < Abc @@a=23 def self.mnp puts "value of a in Pqr a=#{@@a}" end end p=Pqr.new Abc.xyz Pqr.mnp

**output:**

value of a in Abc a=23

value of a in Pqr a=23

in above code @@a is a class variable. And we get the value of a is 23 by calling definition xyz of class Abc.But it should be one as in class Abc value of a is 1. Now , if we use a as class instance variable , then the value of a in class Abc will not be changed by other classes.

class Abc @a=1 def self.xyz puts "value of a in Abc a=#{@a}" end end class Pqr < Abc @a=23 def self.mnp puts "value of a in Pqr a=#{@a}" end endp=Pqr.new

Abc.xyz

Pqr.mnp

**Output:**

value of a in Abc a=1

value of a in Pqr a=23

As we have used class instance variables, the new copy is created for both the classes.

hope, you are enjoying the journey of learning Ruby! All the best..!!