Tuesday, 11 November 2014

Just Arrays in Ruby

arr1= Array.new(10)
str1= "vicky ruby python qtp"
arr2= str1.split(" ")
puts arr2.inspect

puts arr1.size
puts arr2[0]

puts arr2.size
puts arr2[3]
arr1[0]="Testing"

puts arr1[0]

for i in (1..5)
  arr1[i-1]="value: "+ i.to_s
end
puts arr1.inspect

for ele in arr2
  puts ele.capitalize
end


# "+++++++++++++++++Array Declaration 2+++++++++++++++++++++++++++++++"
arr3= Array[5,8,6,7,9,3,4]
puts arr3.inspect
puts "Array arr3 size: #{arr3.length}"
puts arr3.sort.inspect    #[3, 4, 5, 6, 7, 8, 9]
puts arr3.sort().reverse.inspect  #[9, 8, 7, 6, 5, 4, 3]

arr4= ['x', 'z','o', 'p']
puts "is arr4 an array? - #{arr4.is_a?(Array)}"
puts "The class of arr4: #{arr4.class}"

puts "Join the array by '/' = #{arr4.join("/")}"  # it returns a string ..

dig=Array(1..9)
puts dig.inspect
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

puts dig[1, 2].inspect
puts dig[-1]
puts dig[1..5].inspect    #arr[range]

ar= [1,2,3,4]
puts ar.inspect
ar.clear    #when cleared, the will be cleared..
puts ar.size
puts ar.class
ar=[]
puts ar.size
puts ar.empty?


arr1= ['welcome', 'to', 'ruby']
for each in arr1
  puts each
  if each.casecmp("TO")
    puts "casecmp working.. ignores the case"
  end
  if each.eql?("tO")
    puts "eql? working.. ignores the case"
  else
    puts "eql? method doesn't ignore the case.."
  end

  if each.include?("by")
    puts "include working.. It's case insensitive."
  end
end


#Output:


["vicky", "ruby", "python", "qtp"]
10
vicky
4
qtp
Testing
["value: 1", "value: 2", "value: 3", "value: 4", "value: 5", nil, nil, nil, nil, nil]
Vicky
Ruby
Python
Qtp
[5, 8, 6, 7, 9, 3, 4]
Array arr3 size: 7
[3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3]
is arr4 an array? - true
The class of arr4: Array
Join the array by '/' = String
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3]
9
[2, 3, 4, 5, 6]
[1, 2, 3, 4]
0
Array
0
true
welcome
casecmp working.. ignores the case
eql? method doesn't ignore the case..
to
casecmp working.. ignores the case
eql? method doesn't ignore the case..
ruby
casecmp working.. ignores the case
eql? method doesn't ignore the case..
include working.. It's case insensitive.

Process finished with exit code 0

No comments:

Post a Comment