Giter Site home page Giter Site logo

rubysyntax's Introduction

Basic syntax for Ruby ๐Ÿ’Ž

There are two methods to open a file in Ruby:

File.new method : Using this method a new file can be created for reading, writing or both. File.open method : Using this method a new file object is created. That file object is assigned to a file. Difference between both the methods is that File.open method can be associated with a block while File.new method can't.

Example

f = File.new("fileName.rb")

Or,

File.open("fileName.rb", "mode") do |f|

Common modes in I/O port

  • "r": read-only mode is the default mode starts at beginning of file.
  • "r+": read-write mode, starts at beginning of file.
  • "w": write-only mode, either creates a new file or overwrites an existing file content.
  • "w+": read-write mode, either creates a new file or truncates an existing file for reading and writing.
  • "a": write-only mode, if file exists it will append the file othrwise a new file will be created for writing only.
  • "a+": read and write mode, if file exists it will append the file othrwise a new file will be created for writing and reading.

Read a file

There are three different methods to read a file.

To return a single line

f.gets
code...

To return the whole file after the current position

f.read
code...

To return file as an array of lines

f.readlines
[code...]

Rename a file

File.rename("olderName.txt", "newName.txt")

Delete a file

File.delete("filename.txt")

Error handling

All exceptions and errors are extensions of the Exception class.

The "begin - rescue"

begin-rescue block is similar to try..catch in JS

begin
  # code may cause error
rescue => e
  # catch error and store it in variable "e"
end

Module

A Module is a collection of methods, constants, and class variables. Modules are defined as a class, but with the module keyword not with class keyword.

rubysyntax's People

Contributors

chuongtang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.