Saturday 14 September 2013

How to use/implement Array in Ruby

                                                 Array           

 

 Array is a collection of same "Data Type" or you can say it is a systematic arrangement of objects of same data type usually in rows and columns.

This tutorial describes "How Arrays can be created and manipulated in Ruby.

Programme  of array


irb(main):001:0> x=[1,2,3,4,5,6]
irb(main):001:0> Put x[2]

Output : 3

Pushing a String Information Array

irb(main):001:0> x=[ ]
x<<"Ruby"


Output :
["Ruby"]


Or

irb(main):001:0> x.push("Ruby")


PoP Information on Array

irb(main):001:0> x=[]
x<<"ruby"
x<<"is"
x<<"best"
put x
puts x.pop
puts x.pop
puts x.pop

Output
=> ["ruby","is","best"]
=>best
=>is
=>1


Join a String

irb(main):001:0> x=["Ruby"," is"," best"," friend"," for"," Hacker"]
irb(main):001:0> puts x.join

Output :
=>Ruby is best friend for Hacker


While loop in Array

irb(main):001:0>x=[1,"Ruby",2,3,4,5]
irb(main):001:0> i=0
irb(main):001:0> while (i<a.length)
  put a[i]. to_s+"x"
    i+=1
end

Output :
Try it

No comments:

Post a Comment