import linecache


def get_content(method, filename):
    """
        Get all method related content from a file
    """
    # We go through the file from the line containing the method definition
    # until its matching 'end' line. We need to keep track of the 'end'
    # keyword appearing in other contexts, e.g. closing other blocks of code.
    remaining_blocks = 1
    current_line = method

    block_tokens = ['do', 'case', 'for', 'begin', 'while']
    content = ""
    while remaining_blocks:
        line = linecache.getline(filename, current_line)
        content += line
        tokens = line.split()

        # If we have a line that requires a matching 'end', we increase the
        # number of blocks.

        if tokens:
            if any(token in tokens for token in block_tokens) or (tokens[0] in ['if', 'unless']):
                remaining_blocks += 1

        # Likewise, if we found an 'end', we decrease the number of blocks.
        # When it gets to zero, that means we have reached the end of the
        # method.

        if 'end' in tokens:
            remaining_blocks -= 1
        current_line += 1
    content += linecache.getline(filename, current_line)

    return content