hl

Sunday, 12 March 2017

Ruby Programming Language Tutorials Lec 04

Tutorial 4


Every thing is a class in ruby

     In ruby every thing is a class to see which thing is in which class just type puts for printing and followed by .class. For example for a number write 1.class for a float number write 1.234.class and for string "A string".class

                          Input                                                             Output



Constant 

     In ruby a constant is defining by staring it by a capital letter. For example: If we create a num by name A_CONSTANT and give it a value 31.4 and again change it by 1.6 it will show you the error but after that it will change the value also. Let us see :-


                          Input                                                             Output

 






File handling

     In ruby file handling is also so easy . Such as for creating a new file we is to type write_handeler = File.new for new file and passing 2 parameters in braces (name of file" yoursum.out " , type of operation "w") now the file is open .you can write into this file . For writing some text into it you is to write write_handler.puts("Random Text").to_s.After writting to it we is to close the file if the file is not closed it will continue to write to it .For closing the file type write_hander.close
      For read data from file there is a command data_from_file = File.read("yourSum.out") and printing it on screen write command puts "Data From File : " + data_from_file

                           Input                                                        Output


No comments:

Post a Comment