Making separate File

Discussion in 'Ruby' started by Michel Son, Jun 10, 2008.

  1. Michel Son

    Michel Son Guest

    Hello,
    The given two classes (TestClass1 and TestClass2) are in a single ruby
    code. I want a code that separate both classes and save them into 2
    diffrent ruby files.

    E.g

    Orginal code "test.rb" (contains the classes)
    after executing

    one file for 1 class "class1.rb"
    second file for second class "class2.rb"
    and so on...

    I would be very thankful to you dear
    -------------------------

    class TestClass1

    def method1(a,b)
    sum = a + b
    puts sum
    end

    def method2(*c)
    l = c.length
    puts l
    end
    end
    --------------
    class TestClass2

    def method1(a,b)
    sum = a + b
    puts sum
    end

    def method2(*c)
    l = c.length
    puts l
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Michel Son, Jun 10, 2008
    #1
    1. Advertising

  2. 2008/6/10 Michel Son <>:
    > The given two classes (TestClass1 and TestClass2) are in a single ruby
    > code. I want a code that separate both classes and save them into 2
    > diffrent ruby files.
    >
    > E.g
    >
    > Orginal code "test.rb" (contains the classes)
    > after executing
    >
    > one file for 1 class "class1.rb"
    > second file for second class "class2.rb"
    > and so on...
    >
    > I would be very thankful to you dear


    For what exactly? I mean, opening two files in an editor and copy +
    pasting the code there can't be that hard, can it?

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Jun 10, 2008
    #2
    1. Advertising

  3. Michel Son

    Michel Son Guest

    Robert Klemme wrote:
    > 2008/6/10 Michel Son <>:
    >> second file for second class "class2.rb"
    >> and so on...
    >>
    >> I would be very thankful to you dear

    >
    > For what exactly? I mean, opening two files in an editor and copy +
    > pasting the code there can't be that hard, can it?
    >
    > Cheers
    >
    > robert



    Without opening them. It should seprate the code based on "class"
    keyword and storing it in other file using ruby code.
    --
    Posted via http://www.ruby-forum.com/.
     
    Michel Son, Jun 10, 2008
    #3
  4. Michel Son

    Axel Etzold Guest

    -------- Original-Nachricht --------
    > Datum: Wed, 11 Jun 2008 01:11:42 +0900
    > Von: Michel Son <>
    > An:
    > Betreff: Re: Making separate File


    > Robert Klemme wrote:
    > > 2008/6/10 Michel Son <>:
    > >> second file for second class "class2.rb"
    > >> and so on...
    > >>
    > >> I would be very thankful to you dear

    > >
    > > For what exactly? I mean, opening two files in an editor and copy +
    > > pasting the code there can't be that hard, can it?
    > >
    > > Cheers
    > >
    > > robert

    >
    >
    > Without opening them. It should seprate the code based on "class"
    > keyword and storing it in other file using ruby code.
    > --
    > Posted via http://www.ruby-forum.com/.


    Michel,

    supposing that the original code is in "sourcefile", and that there is no text in between
    the classes, you can use something like this:

    text=IO.readlines(sourcefile)
    classes=text.split(/(?=class )/).delete_if{|text| /^class/.match(text)==nil}
    classes.each_with_index{|c_text,i|
    f=File.new("class_file_" + i.to_s + '.rb',"w")
    f.puts c_text
    f.close
    }

    Best regards,

    Axel
    --
    GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
    Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx
     
    Axel Etzold, Jun 10, 2008
    #4
  5. Michel Son

    Michel Son Guest

    This error is arises when i run the code:
    "private method `split' called for #<Array:0x28402b8> (NoMethodError)"

    Actually i am new to ruby thats why i have


    --
    Posted via http://www.ruby-forum.com/.
     
    Michel Son, Jun 10, 2008
    #5
  6. Michel Son

    Axel Etzold Guest

    -------- Original-Nachricht --------
    > Datum: Wed, 11 Jun 2008 02:24:45 +0900
    > Von: Michel Son <>
    > An:
    > Betreff: Re: Making separate File


    >
    > This error is arises when i run the code:
    > "private method `split' called for #<Array:0x28402b8> (NoMethodError)"
    >
    > Actually i am new to ruby thats why i have
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    Michel,

    sorry, that was my fault.
    You must change the line with IO.readlines to

    text=IO.readlines(sourcefile).to_s

    Otherwise, text is an Array, but with to_s, it is converted to a String.
    And that's what the Regular Expression needs.

    Best regards,

    Axel

    --
    Psssst! Schon vom neuen GMX MultiMessenger gehört?
    Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
     
    Axel Etzold, Jun 10, 2008
    #6
  7. On 10.06.2008 19:37, Axel Etzold wrote:
    > -------- Original-Nachricht --------
    >> Datum: Wed, 11 Jun 2008 02:24:45 +0900
    >> Von: Michel Son <>
    >> An:
    >> Betreff: Re: Making separate File

    >
    >> This error is arises when i run the code:
    >> "private method `split' called for #<Array:0x28402b8> (NoMethodError)"
    >>
    >> Actually i am new to ruby thats why i have


    > sorry, that was my fault.
    > You must change the line with IO.readlines to
    >
    > text=IO.readlines(sourcefile).to_s


    Or just do

    test = IO.read(sourcefile)

    Cheers

    robert
     
    Robert Klemme, Jun 10, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page