TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Let's Write Some Bad Ruby

64 pointsby adennis4over 9 years ago

11 comments

ricardobeatover 9 years ago
I (frontend&#x2F;javascript developer by trade) wholeheartedly recommend learning the standard unix tools - bash, sed, grep, awk, xargs - for these tasks.<p>While the syntax is daunting at first, and escaping will be an eternal problem, eventually it starts making sense and you&#x27;ll be able to do <i>anything</i> in seconds. The work in the post becomes trivial:<p><pre><code> mv app&#x2F;views&#x2F;foo&#x2F;**&#x2F;* app&#x2F;views&#x2F; sed -i &#x27;s&#x2F;\([a-z]*\)foo_\([a-z]*\)path&#x2F;\1\2path&#x2F;&#x27; app&#x2F;views&#x2F;**&#x2F;* </code></pre> or in a more imperative (and probably less efficient) style:<p><pre><code> for file in app&#x2F;views&#x2F;foo&#x2F;**&#x2F;*; do sed -i &#x27;s&#x2F;\([a-z]*\)foo_\([a-z]*\)path&#x2F;\1\2path&#x2F;&#x27; $file new_path=$(sed &#x27;s,foo&#x2F;,,&#x27; &lt;&lt;&lt; $file) mkdir -p $(dirname $new_path) mv $file $new_path done</code></pre>
评论 #10456300 未加载
评论 #10455472 未加载
评论 #10456803 未加载
评论 #10455275 未加载
jcummings86over 9 years ago
As the author I thought I&#x27;d comment:<p>I wrote this article, because when I was a very new developer, I was learning a lot more Rails than I was Ruby. It had never occurred to me to use Ruby for scripting until I paired with a well rounded Rubyist. With the influx of people learning Rails, I&#x27;ve talked to many people who know much less about Ruby than I did when I was starting out. This isn&#x27;t mentioned in the article at all, but it was definitely something I thought about when I decided to write it. Honestly, if this helps push one new dev to go out and decided to actually learn some extra Ruby outside of Rails, I&#x27;ll be happy.<p>I agree 100% about the shell comments. I know enough shell to do my job, but for me it&#x27;s quicker to write a Ruby script. Not saying it&#x27;s the most efficient way, but it is a way, and it&#x27;s more efficient than doing it by hand. As much as I&#x27;d love to be a unix shell wizard, I&#x27;d rather be improving my Javascript, or learning more Elixir.<p>As for the namespace vs scope: The example was just a way to end up with a scenario where we needed to change some text. Plus, for clarity, if I don&#x27;t want my controllers or URL namespaced, my routes and files should reflect that, not work around it with a scope unless necessary for some reason(in my opinion). I actually later thought that I should&#x27;ve used the script on a JS project, just to help highlight the fact that this is totally independent of Rails.
DIVx0over 9 years ago
I have a scratch folder full of stuff like this written in whatever scripting language is closest at hand, ruby, perl, python, etc. I&#x27;d wager most people here would have something similar.<p>I wonder if it would be an interesting project to comb though it all and see if any useful tools could be extracted. Probably not, like the code in this blog most of my one-offs are unashamedly terrible.
评论 #10455093 未加载
评论 #10454632 未加载
评论 #10456967 未加载
评论 #10455214 未加载
jgwmaxwellover 9 years ago
Whilst not entirely the point of the article, this could have been accomplished without moving any files by changing the route definition to a scope from a namespace in Rails...
评论 #10454974 未加载
评论 #10454872 未加载
lighthawkover 9 years ago
This is the sort of thing a combination of bash&#x2F;grep&#x2F;awk&#x2F;sed is good for also.
评论 #10454224 未加载
评论 #10454412 未加载
smithkl42over 9 years ago
I don&#x27;t know that I could have asked for a better explanation as to why my preferred environment is C#, Visual Studio and Resharper.
评论 #10455912 未加载
stevepikeover 9 years ago
Another awesome tool if you&#x27;re using emacs is wgrep mode. You get the results of `grep` in a buffer, can edit it like any other text in emacs, and then save writes back to all the original files. Really cool.<p>Emacs macros are wonderful too.
bad_loginover 9 years ago
Before i thought mastering regexp were a must to have in a day to day programmer job to quickly modify data with different syntax.<p>Now i am still convinced that regexp are still usefull but you will not get it right for the first try due to syntax corner case. The best solution in my opinion is to have easy to use parsers including one for your main programming language.<p>Actually i emacs with its macro feature (not elisp) + racket with regexp (didn&#x27;t mention earlier but having a strong repl for this kind of incremental script is also a must to have) and sometimes parsers.
Ch_livecodingtvover 9 years ago
This probably was the start of writing ruby. I&#x27;d recommend you guys watch this video of someone writing bad ruby in contrast. <a href="https:&#x2F;&#x2F;www.livecoding.tv&#x2F;video&#x2F;rails-refactoring&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.livecoding.tv&#x2F;video&#x2F;rails-refactoring&#x2F;</a>
gkyaover 9 years ago
If these lot spend two minutes to understand that code at the end, I&#x27;d not depend them for my code. He&#x27;s just processed some text, that&#x27;s all, don&#x27;t bother reading.
tyhoover 9 years ago
Or use a language with good tooling: <a href="https:&#x2F;&#x2F;godoc.org&#x2F;golang.org&#x2F;x&#x2F;tools&#x2F;cmd&#x2F;gorename" rel="nofollow">https:&#x2F;&#x2F;godoc.org&#x2F;golang.org&#x2F;x&#x2F;tools&#x2F;cmd&#x2F;gorename</a>