Ruby Programming Tutorial - Lesson 27 - working with Files

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@bilal-haider·
0.000 HBD
Ruby Programming Tutorial - Lesson 27 - working with Files
## 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](https://steemitimages.com/DQmZvZhz4iqHtjpNUMqw9DRk2etroxnrA6y4hDWMCX9f73g/Screenshot%20from%202018-03-19%2001-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](https://steemitimages.com/DQmVFMbbdM2o7KwyJSwNe35cufn45q5FGmzyq9gayMGvp7U/Screenshot%20from%202018-03-19%2000-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](https://steemitimages.com/DQmXkhhvgujTXBFZuiooZcsSZeTjVxPnzWmiJpdw2ZayTkD/Screenshot%20from%202018-03-19%2001-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](https://steemitimages.com/DQmZnUtzAi9roNZuvLvdqbCu3XdR8PQyuwsQswunnWuZ85X/Screenshot%20from%202018-03-19%2001-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](https://steemitimages.com/DQmQxogvWhHDcYB2GcaGnrhSv8nxZYs8qz2SbCw4z1aNMPo/Screenshot%20from%202018-03-19%2001-34-38.png)
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,