Run Ruby program and compile sources using Notepad++

Notepad++ is for sure one of the most popular text editing software of the present and in this post I will show you how you can use it to run Ruby program and compile sources. It is preferred by most of the programmers and software & web developers due to an extended use over different platforms and its capability to offer features like syntax highlighting for most programming languages. It can also be configured to run external tools such as Java, Ruby or C++ source files.

This piece of software is an open-source software meaning it is free, but this won’t be the only thing that this program has to offer. It has many options that you have to experience sooner or later and you won’t be disappointed.

To make Notepad++ work with different software, compile different source codes with it, you will have to manipulate its built-in environment variables. But first let’s talk a bit about Ruby, which is the main focus of this article, to be more specific the possibility to compile Ruby source files with the help of Notepad++.

If you need full features of an IDE, such as debugging for Ruby then you are probably using one of the following Integrated Development Environments: Eclipse, Aptana Studio, NetBeans 6.9.1 and earlier versions, RubyMine from JetBrains, Redcar or IntelliJ IDEA.

However, if you are writing small Ruby programs and you don’t need such features, one of the best options would be to use the light weight code editor Notepad++.

If you don’t have it installed yet, you can find it by clicking the link in the first sentence of this article and then find the download section. At this moment, the latest version can be found here: http://notepad-plus-plus.org/download/v6.2.html

If you already have it installed, I would recommend at least a reinstallation over your current one (the uninstall of the previous version is not required) with the sole remark that you should install it using below settings to get a faster approach of having the same settings to a new computer, in case there will be such a need for change. This will avoid any complications and might save you some time and optimize any future change of your computer.

Just download the latest version or use the current installer you already have (the same version you already have, if you kept the installer), use next to confirm each window until you reach this one, which should be by default similar to below window. If not, change the settings accordingly.

Run Ruby programs and compile sources using Notepad++ - Notepad++ Setup

The next window is what actually matters. While all the other components are optional and you can check them depending on your needs, I recommend checking the first option as you can see below, for the same argument stated before which says that if you need to change your computer you only have to copy the installed folder to the new machine in order to have all the customization made to the Notepad++ installation to the new computer too. The basic idea is to click on “Don’t use %APPDATA%” to have it checked.

Run Ruby programs and compile sources using Notepad++

Then complete the process with install, next and finish or close, depending on which of these options will follow.

If you didn’t install Ruby by now, it is time to do it. You can get a copy of it by accessing: http://rubyinstaller.org/downloads/ and downloading the latest version.

Regarding installation, it is as simple as any installation, with a little remark that you should follow the steps until you reach the window below where you should click to select the first two options: “Install Tcl/Tk support” and “Add Ruby executables to your PATH”.

The first option, Tcl/Tk support will allow you to create GUI applications in Ruby.

The second option that says it will add Ruby executables to your PATH does exactly what it says: it will add a path to your system PATH variable so that you can use Ruby interpreter and programs from any command window.

Run Ruby programs and compile sources using Notepad++ - Ruby destination

Note: If you already have Ruby installed and you don’t have the value added to PATH for it, you should either add it manually using below instructions or reinstall the Ruby installer using above options.

Regarding the Ruby installation, in order to complete, you have to click install and then finish button and the process of installation is complete.

Run Ruby program – Manually add Ruby location to System Environment PATH

To manually add the Ruby location to system PATH, go to Start – Run – type “sysdm.cpl” without the quotes. Navigate to Advanced in the tabs and then go to “Environment Variables” button. At system variables, in the bottom section of the window, find PATH variable, select it and click edit.

Run Ruby programs and compile sources using Notepad++ - System properties

For me, as you can see above it is already added but for you, if you don’t have it, you will have to add the same thing, assuming you have used the default folder for installation. Else, locate the path to the bin folder inside the installation directory and add it instead of mine.

Run Ruby program – Configure Notepad++ for editing Ruby programs

Open Notepad++, select Settings – Preferences. Click on Language Menu/Tab Settings.

Under “Available Items” you will see supported programming languages. You should also know you can disable the syntax highlight for different programming languages from here by adding them to the “Disabled Items” column/list ( alternate solution to disabling languages is to edit langs.model.xml and stylers.model.xml and comment any lines that make references to the programming language you are trying to remove highlight for ).

Run Ruby programs and compile sources using Notepad++ - Ruby preferences

To continue with our guide, under the ”Tab Settings” for Default modify the tab size to 2 by clicking it and typing in the new value and confirming with enter. Also, click the checkbox “Replace by space”, you should have it “enabled” so that you can use spaces instead of tab character.

Next step is to add a custom menu in the Run menu of Notepad++ for the Ruby programs so that you can actually run Ruby programs directly from the text editor.

From the Notepad++ menu click Run – Run which opens up the following window:

Run Ruby programs and compile sources using Notepad++ - Ruby run

In the above text field write the following command:

cmd /K ruby “$(FULL_CURRENT_PATH)”

Note: You don’t have to replace FULL_CURRENT_PATH, just copy/paste the dull command as above written. It will be automatically read from System Environment PATH.

In order for this to work you should have Ruby 1.9.x installed and the Ruby bin location added to PATH in system environment.

When you have the command typed in, click on save button and the following window will appear:

Run Ruby programs and compile sources using Notepad++ - Ruby shortcut

Now you have to select any key you wish to use in order to run Ruby programs from the pick list. Due to historical events for me, I am used to F5 as you can see above, but you can pick anything including a combination of keys like CTRL, ALT, SHIFT (you have to check them) and something.

Enter a name for the shortcut in the “Name” field, for example “Execute Ruby Program ” and then confirm with OK.

If you click now Run from the Notepad++ menu, you will see your newly created shortcut to be something like this:

Run Ruby programs and compile sources using Notepad++ - Execute Ruby

Before you can actually use this shortcut you will have to save the program as a ruby file to have an actual location, a store location. In other words, if you don’t have a physical file with .rb extension, when you use your shortcut, you will just get an error saying something like “there is no such file or directory (LoadError)”.

To bypass this, after you write your code save it to a file with a name and then use the shortcut. You can see an example below to test it if it is working or not.

Run Ruby programs and compile sources using Notepad++ - Ruby hello

As you can see, I created a file called test.rb and written down inside it below text as being the code to a simple program. Then I pressed F5 on my keyboard and confirmed the command to run (the code we used to add to create the shortcut in first place):

Puts “Hello World!”

This will return a command window as in the next image:

Run Ruby programs and compile sources using Notepad++

Hope this helps any of you reading it and do tell me if you would like to know some other things regarding how to run Ruby program, maybe another example or problems you’ve encountered. I will also try to add more details as there are a few more tweaks you can do in Windows to optimize the work with Ruby.

VN:F [1.9.22_1171]
Rating: 4.4/5 (9 votes cast)
Run Ruby program and compile sources using Notepad++, 4.4 out of 5 based on 9 ratings

Author: bitpsychobyte

Share This Post On

21 Comments

  1. IF IT IS NOT WORKING FOR YOU: When you copied ‘cmd /K ruby “$(FULL_CURRENT_PATH)”’ please replace the ” character manually (twice) as it is not copied correctly.

    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
    Post a Reply
    • Hello Guys, First I’d like to say thank you for all your help. I’m a first time user and trying to understand how to make a path with Notepad ++ and ruby. I followed the instruction above but am still having problems. I think my problem is with my not understanding how and or where to save a ruby file with the .rb extension in notepadd++ because I have done all of the steps but can’t seem to not get either a syntax error or ruby not seeing the file at all. I’m running ruby 1.9.3. I went into the installer bin but DID not see a path there so i did everything manually.

      Thanks guys

      VA:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
      Post a Reply
  2. Hello..

    I am using an in-house ruby framework to test signalling of voip calls. While working on the same we have to store the ruby scripts in a project folder which is a so called enviornment needed .

    For running any script , we have to run it with a predefined command = srun “scriptname” in the project folder.

    Is there any way of doing so directly from notepad++.

    Thanks

    Sukalyan

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    Post a Reply
  3. It’s the double-quotes that are the issue. If you copy and paste you pick-up left and right double quotes and not just normal double-quotes. This is common in WordPress sites.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    Post a Reply
    • Hehe, thanks for sharing, didn’t quite take that into consideration, damn WordPress. :-)

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
      Post a Reply
  4. Simple, straight to the point and useful

    Thanks a lot!

    ;)

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    Post a Reply
  5. By the way, I must thank again Bitpsychobyte for this badass article as well as for writing this really nice N’ useful blog.

    This is the code that worked for me:

    puts ‘What is your nickname or favorite movie/game character?’
    nickname = gets.chomp
    puts ‘Hi there, ‘ + nickname

    Thanks again!
    Yu

    ———–
    P.S.
    I’m just curious what does the actual tech business requires as language, you just told me that you did not use Ruby in your daily jobs.

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    Post a Reply
    • Well, I’m not sure I’m the best guy to answer to this question as in my daily job I am a network administrator with a bit of a more expanded responsibilities into the system administration area. That’s how I got in touch with Ruby, by experimenting on some projects.

      As for what tech businesses languages are preferred, it all depends on the area of interest for that specific business or for the ones that hire/seek programmers and it is always changing. As a trend I saw a lot of people heading towards Java the last months, with an increased activity on the Java and Eclipse topics.

      The thing is that if you really want to know what the market is searching for, if this was the real question and I got it well, I would scout forums that focus on programming, like the ones below but not only:

      http://forums.devshed.com/
      Codecall.net

      I hope you will find more details on this topic and maybe share it too if anything interesting pops up. Good Luck! :-)

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
      Post a Reply
      • Thanks, I really enjoy your input on the subject!

        VA:F [1.9.22_1171]
        Rating: 0 (from 0 votes)
    • I must say your code works really well, please ignore previous comment.

      I just wanted to add that there could be an automatic exit after the test
      cmd /k $(FULL_CURRENT_PATH) & pause & exit

      Kudos for the article!

      VA:F [1.9.22_1171]
      Rating: +1 (from 1 vote)
      Post a Reply
      • Well, I wanted something to represent the “basic” stuff but your feedback is appreciated for those who want to try something else.

        Thanks for the heads up. :-)

        VA:F [1.9.22_1171]
        Rating: +1 (from 1 vote)
  6. This was a great tutorial for me! Loved it because this was exactly the sort of page with a how-to that i was looking for. However, when i get through all the steps, it still returns this message:

    “ruby: Invalid argument — A’C:/Users/blah blah blah/Ruby.rbA (LoadError)
    C:Program Files (x86)\Notepad++>”

    Any help here would be awesome!
    Thanks, Ian

    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
    Post a Reply
    • I’m glad you liked it and sorry that it didn’t apply to your case.

      Could you try to compile the code without using a Ruby archive file ( .rba ) ? In other words, could you save the file into a .rb file and then execute it ?

      Also, did the installation of the Ruby program added the correct path to the system environment ?

      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
      Post a Reply
      • I get the same error message as Ian. It doesn’t matter what file extension I use and the I have the correct path in the system environment. Any idea what else it could be? Thanks!

        VA:F [1.9.22_1171]
        Rating: 0 (from 0 votes)
      • Might be something wrong with the code but I can’t seem to find what. The thing is that was the most simple instruction I could come up with to test the result of the settings.

        My main passion is not really programming but in my daily job I somehow touch this domain too so I wanted to write this article because I knew at least some will get something useful out of it.

        Could you do me a favor and try something and tell me if after running below code does it work for you?

        print “What is your nickname or favorite movie/game character ? ”
        nickname = gets.chomp
        puts “Hi there, #{nickname}!”

        One more thing, please try with and without the shortcut. So once it should be “Run” – “Execute Ruby Program” and the other test with F5 or whatever key you used to bind the action.

        VN:F [1.9.22_1171]
        Rating: +1 (from 1 vote)
      • Reinstalling Ruby while ensuring that the 3 options were clicked. (Tcl/Tk support, Ruby Path, and associate .RB files), fixed my case.

        After a bit of coding and learning I’m seriously considering installing Aptana, NetBeans or Farawla. Executing the code was not the last problem I had using Notepad++…. ^^,

        Good luck!
        From: First time coder.

        VA:F [1.9.22_1171]
        Rating: 0 (from 0 votes)
    • Do not Copy + Paste the HTML code from above : cmd /K ruby “$(FULL_CURRENT_PATH)”
      First Paste it in notepad to clear the HTML special characters and then copy it from Notepad and paste it to the Run Command of Notepad++

      VA:F [1.9.22_1171]
      Rating: +3 (from 3 votes)
      Post a Reply
      • @sandyiit: THIS fixed my problem! (It was the problem Kris got, too.)

        VA:F [1.9.22_1171]
        Rating: 0 (from 0 votes)

We would be glad to receive any feedback from you. Throw it at us, we can handle it!