Ruby Programming Tutorial - Lesson 27 - working with Files

in #ruby8 years ago

Lets learn to create Files

Yes we can create files using ruby programs.

Definition of a File

A File is an abstraction of any file object accessible by the program and is closely associated with class IO File includes the methods of module FileTest as class methods, allowing you to write (for example) File.exist?("foo").

In simple words, File is a class which helps us working with files, we can create files, update files, rename them and we can also delete them .. by having this ability we have endless possibilities of creativity..

images are files, ruby programs are files, word documents are files, excel sheets are files, a block-chain containing blocks information is also a file.. and we can create all of these kinds of files using ruby programs.

Lets start by creating a simple text file :)

_myfile = File.new("beautiful.txt", "w")
_myfile.syswrite("I love Bilal Haider Qureshi :)")                
_myfile.close()                 

Screenshot from 2018-03-19 01-38-48.png

Notice that I don't have any other files, besides file.rb ..
but when I execute my program, it creates a file for me.. with the text which I wanted to write inside of it :)
That's how we create a file :)

Screenshot from 2018-03-19 00-59-19.png

Notice that the File Class is what ruby provides us, to work with files.
Here is a reference link, where you will be able to find more Methods which you can perform on files. :)

http://ruby-doc.org/core-2.2.0/File.html

Now lets create a ruby program. with this Skill

Create a Ruby Program that creates Classes Dynamically, We should be able to call a method, supply it a class name followed by few method names and it should create a file, and inside that file a Class with required methods

Here we have created a Class which can be used to create more Ruby Classes :)
When we create an object out of this class, we pass to it a filename/classname .. and methods which this class needs to have. and it creates a file for us, inside that file it creates a class with methods :)
This is magical ;)

class Rubyclass
    def initialize(filename, *properties)
        @class_name   = filename
        @file_name    = filename + ".rb"
        @method_names = properties
    end 

    def create_class
        puts @file_name
        _myfile = File.new(@file_name, "w")
        _data   = "# This is a Ruby Class #{@class_name} \n"
        _data   += "class #{@class_name} \n"
        
        @method_names.each do |method|
            _data += "\t def #{method} \n"
            _data += "\t # Code goes here \n"
            _data += "\t end \n"
        end

        _data   += "end"
        _myfile.syswrite(_data)
        _myfile.close() 
    end
end

Screenshot from 2018-03-19 01-26-45.png

Here is how we use it :) in our main.rb file.

require_relative 'rubyclass'

## Create a file called Animal.rb, and create a Class inside of it called Animal, that has four methods as mentioned.
_rubyClass = Rubyclass.new("Animal", "speak", "sing", "walk", "run")
_rubyClass.create_class()

Screenshot from 2018-03-19 01-33-27.png

Finally when we execute this program, it creates a file for us, and inside that file a class with required methods :)
We can create as many Classes dynamically as we want, using this program..
We only need to Call 2 lines of code as shown above :) .. This program can save a lot of time. When you need to create 100 Classes maybe .. you can become creative which this code... and store your Class names and methods names in arrays and write a loop that will create all those classes for you .. :)

Screenshot from 2018-03-19 01-34-38.png

Sort:  

hopefully the file is completed in accordance with the targets performed @bilal-haider

I just started programming and learned HTML and CSS. Was in search of brief learning of next language. You made it easy for me in your post. Thanks for this valuable information

Yes :) these articles will help you a lot, because they will take you to Rails which is BEST web application technology :)

very good

I'm beside you
You calm my soul
Setiaamu on your promise
To always take care of me
Because without you what it means to me
Because without you I will not be here
Mother believe me
I'll stay here just for you
Until the star does not shine
Until no more my life
I see your cahya that confirms me
That you are always my light
Be faithful to your promise
To always keep me

un buen poema

good post

I'm beside you
You calm my soul
Setiaamu on your promise
To always take care of me
Because without you what it means to me
Because without you I will not be here
Mother believe me
I'll stay here just for you
Until the star does not shine
Until no more my life
I see your cahya that confirms me
That you are always my light
Be faithful to your promise
To always keep me

please vote my picture

please vote my picture

Excelente e interesante Post. muy didáctico y Útil. Éxitos!!