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

Advertisements

2 thoughts on “file concept in ruby

    • raveendran says:

      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 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