Sunday, 7 September 2014

Arrays in Ruby

a= Array.new(10)
puts a.size
puts a[0].nil?
puts "is array nil: #{a.nil?}"
puts a[0]="testing"
puts a[0] if not(a[0].nil?)   #we can even say: !(a[0].nil?) .. gives the same result..

x= true
y= false
if x and y
  puts "True"
else
  puts "False"
end


b= Array.new
puts "is Array b nil? : #{b.nil?}"
puts "b array size: #{b.size}"   #size is zero here..

c=Array["vicky","ruby"]
puts "c array size is: #{c.size}"   # size is 1 here.. it has one element..

d= Array.[]("RUBY", "EASY", c)
puts "Array 'd' size is: "+ d.length.to_s


e= Array(21..25)
puts "Array e is : #{e}"


f= Array[1, 2, 5, 3, 6,9]
puts "sort array f: #{f.sort}"
p= f.clear
puts "post array clear, array size is: #{f.size}"
puts "join array: #{d.join("/")}"

puts "reverse array: #{d.reverse}"

x=Array[1,2,3]
puts x.size + x[1]

y=[1,2,3,4,5,6,7]
puts y[2]


Output:
10
true
is array nil: false
testing
testing
False
is Array b nil? : false
b array size: 0
c array size is: 2
Array 'd' size is: 3
Array e is : [21, 22, 23, 24, 25]
sort array f: [1, 2, 3, 5, 6, 9]
post array clear, array size is: 0
join array: RUBY/EASY/vicky/ruby
reverse array: [["vicky", "ruby"], "EASY", "RUBY"]
5
3



No comments:

Post a Comment