Code Monkey home page Code Monkey logo

context_manager's Introduction

Context Managers

Python like context mangers in Ruby. Context managers allow you to allocate and release resources precisely when you want to.

Install

gem install context_manager

Write you own context manager

A sample context manager to open and close a file. Similarly, you can write your own.

def open(filename)
  f = File.open(filename)
  
  close_file = finish do |file|
    file.close
  end

  [f, close_file]
end

Calling the context manager

require 'context_manager'

with open('filename.txt') do |file|
  file.read
end

Examples

1. Connect to DB

def connect_db
  # harcoding DB values to keep the example simple
  conn = Mysql2::Client.new(
    host: 'localhost',
    username: 'root',
    password: 'root',
    database: "my_database"
  )
  close_conn = finish { |conn| conn.close }
  [conn, close_conn]
end

Call with the DB context manager

with connect_db do |conn|
  result = conn.query("select * from users limit 1")
  result.first
end

2. Read from a file and write to other file

You can even write a nested context manager. For example, If you want to read contents from a file and write it to some other file.

Context manager that opens file in read mode

def open(filename)
  f = File.open(filename)
  close_file = finish { |file| file.close }
  [f, close_file]
end

Context manager that opens file in write mode

def write(filename)
  f = File.open(filename, 'w')
  close_file = finish { |file| file.close }
  [f, close_file]
end

Call write context manager inside read

with open('read_from_file.txt') do |read_file|
  with write('write_to_file.txt') do |write_file|
    write_file.write read_file.read
  end
end

context_manager's People

Contributors

akshg 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.