mbigras mbigras - 1 year ago 57
Ruby Question

How can I use Ruby regular expressions to search for a path and replace it?

I have a python script:


import sublime, sublime_plugin

class SpeakingToSublime(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("advanced_new_file_new",{"initial_path": "/Users/max/test/test2/"})

and I'm trying to write a ruby script that does the following:

desired_path = "/some/path/i/want/to/go/"
# read speaking_to_sublime.py
# parse through it and store as a string
# store contents of speaking_to_sublime.py in here
speaking_to_sublime_string = ""
# use regular expressions to somehow replace:
# "/Users/max/test/test2/" with desired_path
# write speak_to_sublime_string to speaking_to_sublime.py

I can figure out how to read and write to a file with ruby, but how do I use regular expressions to get to
and replace it with

Answer Source

I was trying to modify the python script because I wasn't sure how to pass arguments to sublime from the command line. As a workaround I was going to make a custom plugin and change the "arguments" by writing them to a file right before I called the command using sublime from the command. It sounds roundabout but I didn't know about escaping the quotes when you pass arguments from the sublime cli yet. I actually haven't testing if it would work yet, but for anyone who is interested the appropriate regex can can found on rubular:

re = /"initial_path": "(.+)"/

the reason why I was trying to use regexs was because the path would be changing all the time since I was essentially planning to re-write the plugin every time it was called. I learned how to do it after watching a wonderful talk by Nell Shamrell