Lusheez
Lusheez

Reputation: 77

Get the source code of a ruby file in the form of a string/text

Is there any simple way to get all the source code of a ruby file in the form of a string? I'm looking for something that would behave like inspect.sourcelines(codeObj) in python.

Upvotes: 2

Views: 393

Answers (1)

Sancarn
Sancarn

Reputation: 2824

I'm surprised this hasn't been asked before... But I couldn't find the question on SO anywhere...

myCode = File.read(__FILE__)

That should do the trick.

__FILE__ is a special variable which contains the full file path of the currently executing file. Depending how you script is being executed you might be better off using $0 instead. But this really depends on what you really want.

Upvotes: 4

Related Questions