Ruby on Rails command line looks weird - ruby-on-rails

I just began learning RoR using the materials from railstutorial.org. When the author uses the command line, the resulting text are always nicely organized and colored. When I do the same thing, I often get "weird" characters and unorganized text as result.
http://i.imgur.com/2Q0kzwf.jpg
Example
http://i.imgur.com/mZP4SI9.jpg
My attempt to do the same
I'm not quite sure what to do to make my command line more organized like the one shown in the tutorial. Any help would be appreciated. In case you need to know, I'm using Windows 7.

It is because by default Windows command prompt doesn't know about ANSI color Sequences. You can try https://github.com/adoxa/ansicon which is supposed to make it aware. The reason the tutorial authors look like that is they are using a *nix shell that understands the color codes (probably bash or zsh on either linux or osx if I had to guess)

As Doon pointed out, the Windows console doesn't recognize ANSI sequences. As suggested your best bet right now is to use external tools.
However, since there are some pain points using most of the external tools, Ruby 2.0 will provide support for ANSI escape codes in Ruby out-of-the-box without having to depend on external gems or tools.

Related

Does every ruby on rails app need to be written on the console terminal?

I don't like using the console terminal. Is it possible to use a text editor like bracket *(MY FAVORITE) to write ruby code? I've seen ruby files. Are those written on an editor? Or is it the case that all these schools teach you the basics through the console and later you can write code on an editor?
Ruby do not have to be written in the terminal.
I myself do most of my coding in sublime and do run some commands in the terminal to run tests and generate some files. (Rails generators are quite awesome by the way)
I have a colleague who uses Ruby mine and from what I understand you can use that to do rails development without using the console.
Yes obviously you can use text editors.
You can write code in editor save it as file_name.rb and run from console as ruby file_name.rb
And this is just a start with Ruby. Just give your 20 minutes and you will get to know
https://www.ruby-lang.org/en/documentation/quickstart/
When you will get familiar with all the great things about ruby
You can start with Rails, as it's name suggests it actually get you started
http://guides.rubyonrails.org/getting_started.html
Just go on and you get fell in love with Ruby on Rails what it is called.
It's a lot convenient to use text editors and write codes instead of giving yourself a hard time coding using the console.
Sublime Text is one of the widely used editors, very easy to use and free. I've always used this.
You could also check out some of the best editors here 5 Best editors - Lifehacker
You can use any text editor and save the codes with the .rb extension <file-name>.rb and run it through terminal like this: ruby filename.rb

Find all uses of a method in vim

Is there a way to find all uses of a method in vim? I'm using vim as an IDE for Rails with rails.vim. ctags helps to jump to a method definition from usage but not the other way round AFAIK. I'd like to be able to find everywhere (controllers, views etc) that a method has been used.
There aren't any perfect solutions for this in Vim, but you can get close with cscope and grep or ack.
cscope will help you find all references to a symbol. It's made for C and C-like languages, but it does a decent job of matching symbols in Ruby code as well. It's not going to get the context right all the time.
Here's a vim cscope tutorial and a blog post about Ruby/Vim/cscope, and another blog post, both of which include additional tips about navigating Ruby/Rails code in Vim.
Using grep or ack from Vim with quickfix integration is another great way to find symbols. They've got no notion of scope/context, but often a simple search is enough. Using just the built-in :grep command, you can do:
:grep some_method app/controllers
:cwindow
And get the results of the search in the quickfix window, which will allow you to quickly navigate to the matching files and line numbers.
A much better option is the ack.vim plugin, which integrates ack with Vim and makes use of the quickfix window.
If you're not already using a plugin for navigating CTAGS, I recommend Tagbar.
I wrote a gem to do just that: https://rubygems.org/gems/starscope
It parses ruby code properly and exports to ctags and cscope file formats.

How do I incorporate an independently maintained ruby script into a rails application

I have a ruby script (.rb) that opens a file and makes a csv file after some parsing.
I maintain the script independently and may use in other applications.
Right now I just copied and pasted the code into my controller... I know that isn't right!
How am I supposed to incorporate this ruby script to my application?
Do I make it a gem?
Thanks.
Making it a gem and installing it is one option. Otherwise, register the directory of the file as load path. That part may depend on the operating system. For example, with Ubuntu Linux, I do in the terminal:
export RUBYLIB=path_to_the_directory_where_the_file_is
Then, require that file and use it. When you want your library to behave differently depending on if it was called directly from the command or from another ruby script, the common way is to write in your library:
if __FILE__ == $0
commands_to_execute_when_called_directly_from_command
end
We write little Ruby-based command-line tools all the time, and treat them as regular Linux apps. It's trivial to call them using back ticks or %x, or chain them using regular pipes (|) as we would a regular app.
If we're going to be throwing a lot of data around, often we'll add a --json flag using OptionParser, which lets us emit JSON to the calling program, making it easier to reuse the data instead of having to parse CSV or columnar data.
You can install those sort of apps in /usr/local/bin on a *nix system, make sure the path is set to search there, and then share the code among shell, Ruby or any other language capable of using a sub-shell.
Just because they're written in Ruby doesn't mean they have to be a gem or module. Ruby is capable of much more than that and fits into the usual host ecology well.

tips and tricks for using vim with ruby/ruby on rails

I'm one of those developers who isn't using TextMate with any of his Ruby/Ruby on Rails work. My particular loyalty in this arena lies with vim. What are your favorite tips/tricks for using vim with Ruby and/or Ruby on Rails to make you as efficient as possible when working?
Most important
Get a copy of rails.vim it is awesome on millions of levels. Read the doc. There are way too many tips, :Rview customer, :RSmodel foo, :Rinvert, gf, :Rextract, :Rake and the list goes on and on. You will probably want NERDTree as well for easy navigation (which you can access using :Rtree)
Second most important
Follow tpope on twitter (the author of fugative, rails.vim, haml.vim, vividchalk theme, cucumber.vim and so on), he seems to be posting new related to Rails vim plugins quite regularly (be it syntax highlighting or git integration).
You might want to checkout my ruby/rails specific vimfiles.
Its a useful starting point and has many useful Ruby/Rails plugins bundled and configured.
The one thing that really sucks about Textmate is that it doesn't run on Linux. My vim/gvim config is the same on Mac, Windows and Linux. Same fonts, same themes, same plugins and same customizations.
I mostly use Textmate for snippets and quick evaluations for posting here.
I wrote an in depth guide on using Textmate features (especially Rails related features) in VIM. It's very relevant to this question.
http://www.jackkinsella.ie/2011/09/05/textmate-to-vim.html
I don't use vim, instead, I'm like those millions of developers using Textmate. Nevertheless, a colleague does use vim/gvim.
By looking at him work, one of the things I wish I could do in Textmate is the ease of working on multiple files at the same time. Basically, you can easily manipulate multiple windows, which is quite handy.

RoR development-environment setup

I'm interested to play around with RoR a bit. Apart from literature i should read, i'm particularly interested about how to setup development environment.
Here's a good example how to setup environment for Java from Noda Time project wiki pages.
I want something similar but for RoR.
As far as i know - unix operation systems fits way much better (have toyed on windows 1 1/2 year ago - pure nightmare). So it would be nice to get some recommendations about linux distros and how to make it run next to win7/xp.
Basically - i want to shorten endless searching and improvisation until i can play with some code.
P.s. bonus for simple sample project. :)
The single quickest way to get up and running might be to simply grab Netbeans and develop against its built-in JRuby instance. It will walk you through setting up a Rails project and even give you some nice-to-haves like autocompletion.
Baring that, the easiest method is likely to download a Linux VM Appliance pre-Customized for Rails Development and a copy of VMWare Player.
The best 2 ways to go are OS X and Linux, I use Ubuntu just because it's the nicest package that I've found (there will no doubt be a variety of opinons on this.)
For Rails, I like to not use the packages or pre-installed versions, and instead build my own so I can test against various versions of ruby if need be. Hivelogic has a nice post about how to do it for OS X 10.6.
From there, you can just run:
rails myproject
cd myproject
./script/generate scaffold post title:string body:text
rake db:migrate
rm public/index.html
./script/server --debugging
And then connect to http://localhost:3000/posts to get to your application.
Once you get more comfortable, check out running Passenger instead, so you can have multiple applications running at the same time. On OSX there's even a nice Prefpane to easily set up new sites. This also ins't too hard in Ubuntu with the examples provided in the passenger docs.
For editing the application I think the IDEs (Aptana, Netbeans, etc.) are still too heavyweight, especially for small starter projects. I like Textmate (like everyone else) for OSX and gedit with gedit-mate.
Once you're writing applications, you'll find that railsapi has the best interface for browsing all of the various methods not only in Rails, but ruby, authlogic, and a bunch of other common gems.
Lastly, you'll want to look into source control, with git being preferred in the Rails community at the moment.
Good luck!
Get VMWare player
Get ubuntu vm
If it's server version - install desktop x or whatever it's called
Mess around with sudos, visudos
Mess around with vi editor to save newly created account to sudoers list
Mess around with vertical mouse scrolling which apparently does not
work on vmware+ubuntu
Finally install netbeans
Through plugins, install ruby on rails
Some global updating
Enjoy toying
tadaaaa...
Something like that i wanted - with each point explained a bit (no doubt that my steps aren't best ones and sounds funny for those who knows).
I guess that i forgot to mention that i lack knowledge of unix systems in general too.
Anyway - got what i was looking for. :)

Resources