Module: Kodekopelli::Util
- Defined in:
- lib/kodekopelli/util.rb
Overview
:nodoc:
Class Method Summary collapse
-
.escape_javascript(javascript) ⇒ Object
Mad props to Ruby on Rails developers for the following methods.
-
.file_contents(location, ignore_if_missing = false) ⇒ Object
Given the location of a file, returns its contents.
Class Method Details
.escape_javascript(javascript) ⇒ Object
Mad props to Ruby on Rails developers for the following methods
26 27 28 |
# File 'lib/kodekopelli/util.rb', line 26 def Util.escape_javascript(javascript) (javascript || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } end |
.file_contents(location, ignore_if_missing = false) ⇒ Object
Given the location of a file, returns its contents.
:call-seq:
file_contents(location) -> contents
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kodekopelli/util.rb', line 9 def Util.file_contents(location, ignore_if_missing=false) unless File.exists?(location) unless ignore_if_missing raise "Could not locate file [#{location}]." end return nil end begin file_handle = File.new(location) file_contents = file_handle.read ensure file_handle.close end file_contents end |