Created
March 28, 2011 19:04
-
-
Save brianm/891050 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
t = <<-EOT | |
#!/usr/bin/env ruby | |
if 1+1 == 2 | |
puts "hello world" | |
end | |
EOT | |
# what one liner replaces '.gsub /^\s*/, "' to make this output "win" | |
s = <<-EOB.gsub /^\s*/, "" | |
#!/usr/bin/env ruby | |
if 1+1 == 2 | |
puts "hello world" | |
end | |
EOB | |
if t == s | |
puts "win" | |
else | |
puts "lose" | |
end |
Do you want to remove the indent? ...or change the header? ...or both?
EOB.gsub(/^\s{2}/,'').gsub(/^#!.*ruby/, "#!ruby")
works.
Of course using the gem as David already suggested is better to remove the indent. But that does not solve the puzzle ;) I would shoot for...
EOB.outdent.gsub(/^#!.*ruby/, "#!ruby")
Remove the indent. The #! thing is a red herring
…On Mon, Mar 28, 2011 at 4:22 PM, tcurdt ***@***.*** wrote:
Do you want to remove the indent? ...or change the header? ...or both?
EOB.gsub(/^\s{2}/,'').gsub(/^#!.*ruby/, "#!ruby")
works.
Of course using the gem as David already suggested is better to remove the indent. But that does not solve the puzzle ;) I would shoot for...
EOB.outdent.gsub(/^#!.*ruby/, "#!ruby")
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/891050
The #! line was a red herring, nt actually looking to solve that, just outdent!
outdent solves it, but invokoed a monkeypatch based library :-(
@tcurdt -- legal, but weak, relies on the 2 spaces :-)
EOB.split(/^(\s*)/, 2).drop(1).each_slice(2).map { |prefix, text| text.gsub(/^#{prefix}/, '') }.first
A better one:
EOB.scan(/^(\s*)(.*)/m).map { |prefix, text| text.gsub(/^#{prefix}/, '') }.first
And the simplest one I could come up with... It takes advantage of the side-effects of matching regexes (matched groups go into $1, $2, ...):
EOB.gsub(/^(\s*)(.*)/m, '\2').gsub(/^#{$1}/, '')
+1 for martint's last version
what a weird problem though :)
Impressive!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
s = t.outdent via "gem install outdent" ? :)
(you don't want #!/usr/bin/env ruby automatically converted to #!ruby, right?)