Simple array programs in ruby


Hai friends,

Last night i learned basic arrays in ruby.At the starting i was little bit struggled. Then learned something in array.

The basic array program for store the input value in a array:


# The simple array program in ruby.Here we'll get 5 input values from the user.The inputs are stored in a simple array and this program will print tha array values.
=begin
Program name: array.rb
Date written: 16.01.2011
Date modified: 16.01.2011
Author: Tha.Suresh
Licence: LPGL
=end

# Main part of the starts here

#!/usr/bin/ruby
name = []
puts "Enter the 5 names "
name[0] = gets.chomp
name[1] = gets.chomp
name[2] = gets.chomp
name[3] = gets.chomp
name[4] = gets.chomp
for i in name
print i, " "
end
puts name

Print the a array in reverse format:


# The simple array program in ruby.Here we'll get 5 input values from the user.The inputs are stored in a simple array and this program will print tha array values as reverse format.
=begin
Program name: reverse_array.rb
Date written: 16.01.2011
Date modified: 17.01.2011
Author: Tha.Suresh
Licence: LPGL
=end

# Main part of the starts here

#!/usr/bin/ruby

name = []
puts "Enter the 5 names "
5.times do |x|
name[x] = gets.chomp
end
for i in 4.downto 0
puts name[i]
end

Print all array inputs expect particular one:


# The array program in ruby.Here we stored the some string inputs in a array.Our motto of this program is, to print the all array values expect the particular one value.

=begin
Program name: print_all_value_expect_particular_one.rb
Date written: 16.01.2011
Date modified: 16.01.2011
Author: Tha.Suresh
Licence: LPGL
=end

# Main part of the starts here

#!/usr/bin/ruby

name = []
name[0] = "thasu"
name[1] = "arul"
name[2] = "shrini"
name[3] = "sathia"
name[4] = "yogesh"
for i in name do
if i == "shrini" then
next
end
puts i
end

One thought on “Simple array programs in ruby

  1. Suresh,

    14 name = []
    15 puts “Enter the 5 names ”
    16 name[0] = gets.chomp
    17 name[1] = gets.chomp
    18 name[2] = gets.chomp
    19 name[3] = gets.chomp
    20 name[4] = gets.chomp

    Instead of getting input like this, you can use loop to get multiple input and can store in array.

    name = []
    5.times do |x|
    name[x] = gets.chomp
    end

    Use smart functions to become a smart programmer.

    For that we need to study many things and have to remember few interesting functions. Then surely you will become smart programmer.

    All the best. Keep Coding….

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