ruby excercise

file concept in ruby

File concept in ruby:

Ruby program
**********

full= File.open(“raveendran.txt”)
phrase=[“phrase1″,”phrase2″,”phrase3″]
count=0
full.each do |line|
first=[]
first=line.split(/\|/)
first.each do |single|
sub=single.strip!
main = (sub).to_s + ” “+(phrase [count]).to_s
puts main
count+=1
end
end

***********************************
raveendran.txt
******

item1 | item2 | item3 | item4 |
item11 | item21 | item31 | item41 |
item12 | item23 | item34 | item45 |
item13 | item23 | item33 | item43 |
item14 | item24 | item34 | item44 |
***********************************
Output
******

>ruby file.rb
item1 phrase1
item2 phrase2
item3 phrase3
item4

item11
item21
item31
item41

item12
item23
item34
item45

item13
item23
item33
item43

item14
item24
item34
item44
>Exit code: 0
*********************************

Regards,

P.Raveendran

2 thoughts on “file concept in ruby

    1. Its tested in Ruby 1.8.4

      For current Version

      full= File.open(‘raveendran.txt’)
      phrase=[“phrase1″,”phrase2″,”phrase3”]
      count=0
      full.each do |line|
      first=[]
      first=line.split(/\|/)
      first.each do |single|
      sub=single.strip!
      main = “#{sub} #{phrase[count]}”
      puts main
      count+=1
      end
      end

Leave a comment