Ruby Programming Tutorial - Lesson 27 - working with Files
ruby·@bilal-haider·
0.000 HBDRuby 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() ```  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 :)  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 ```  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() ```  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 .. :) 
👍 bilal-haider, ashe-oro, jdcrunchman, nanocheeze, xtdevelopment, sanchezpuukko, ozanaydn, echo001, jhosep27, princessjoy, panosgram, krystianos, liraqisyaafina, misabelota, ironclad010, endezyar, damotil, smarmy, jesusvarela, rmdochi14, memox17, elcoco, alimreza451, vb-tech, alimirza0416, thinkandgrowrich, icemanve, lamagrix, breidylara, manuelalejandro5, iassmyy, samiulbd, von96, jrpv55, sam-world, buildawhale, patonggrease, upmyvote, muzam, rianh, heryjunot, derpdragon, cryptopieta,