Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Ruby code to string

I am refactoring some code that I did not write, and I found a line that looks like this (it is much longer, i used just a little bit for this example):

system("rubyw -e \"require 'win32ole'; @autoit=WIN32OLE.new('AutoItX3.Control');")

To increase readability, I refactored it to

do_something =
  "rubyw -e \"
    require 'win32ole'
    @autoit=WIN32OLE.new('AutoItX3.Control')"
system do_something

Then I wanted to make some changes, but since the code I am working on is in a string, I lose syntax highlighting, parenthesis matching and all that good stuff.

Is there an easy way to write some code outside of the string and then convert it to string?

I have searched the web and stackoverflow, but could not find the answer.

For more information, take a look at original code at bret/watir (Watir::FileField#set, line 445), and my fork at zeljkofilipin/watir (lines 447-459).

like image 427
Željko Filipin Avatar asked Dec 18 '22 07:12

Željko Filipin


1 Answers

You can use the following syntax:

do_something = <<SOMETHING
  rubyw -e 
  require 'win32ole'
  @autoit=WIN32OLE.new('AutoItX3.Control')
SOMETHING

Apparently it's a heredoc! You can find another example here(doc).

That's not to say that the command won't freak out about having line breaks in there. However you could likely run it by system do_something.split(/\r\n/).join('') or something like that.

like image 137
wombleton Avatar answered Jan 01 '23 17:01

wombleton