Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, November 22, 2009

How to enable Ruby debug in RubyMine 2.0 on Windows

I recently had the chance to evaluate JetBrains' RubyMine. I was well impressed by this IDE except for the impossibility to debug Ruby code.
However I didn't gave up and, googling a bit, I found a thread inside JetBrains Developer Community that helped me to fix my installation and finally to debug Ruby code inside RubyMine.
Although the entire procedure is fully described in that thread, I think is worth summarizing the fundamental steps to fix the environment.
Let's start from a scenario where Ruby 1.9 has been installed with the one-click installer and RubyMine 2.0 has been installed too. (If you installed Ruby with the zip binary, reinstall it using the one-click installer as found here. That's because currently the ruby debugger ide only works on Windows using a RubyInstaller installation). Also verify that Ruby's root path doesn't include spaces.
  1. Download Ruby DevKit. (The links brings you to a .7z file, that you can open with 7Zip).
  2. Open the .7z file and follow the instructions in INSTALL file: basically they said to extract top-level folders (i.e. bin and devkit) to the Ruby's root folder and to modify the contents of fstab file according to your installation. In my case, my Ruby folder was: C:\programmi\ruby19, so my fstab file contents became:
    C:\Programmi\Ruby19\devkit\gcc\3.4.5\mingw32 /mingw
    C:\Programmi\Ruby19\devkit\msys\1.0.11\usr\local /usr/local
  3. Execute the command gem install ruby-debug-ide19
    (Note that's fundamental to install ruby debug ide as ruby-debug-ide19 because RubyMine looks for a gem with name ruby-debug-ide19 and not ruby-debug-ide, as you would expect). Also be patient because the command takes a while to complete.
  4. Open with a text editor the file command.rb that's part of the installed ruby-debug-ide19 gem. In my case, that file was under folder C:\Programmi\Ruby19\lib\ruby\gems\1.9.1\gems\ruby-debug-ide19-0.4.12\lib\ruby-debug
  5. Modify the beginning of procedure debug_eval (at line 120) so to enter a new statement between statements str = str.to_s and max_time = 10. The new statement to enter is:
    return "" if str == "$FILENAME"

    At the end, the procedure debug_eval should begin like this:
    def debug_eval(str, b = get_binding)
      begin str = str.to_s
      return "" if str == "$FILENAME"
      max_time = 10
      to_inspect = str.gsub(/\\n/, "\n")
  6. Save the file
That's it. Enjoy Ruby debugging with RubyMine!

Sunday, October 12, 2008

youtube-leecher

Here we go! youtube-leecher has just landed on SourceForge and now it's available for download here.

But what's youtube-leecher actually? Well, youtube-leecher is a simple yet powerful Ruby program to download all YouTube videos found inside video containers.

So now what's a "video container"?
From youtube-leecher perspective, a video container can be basically a HTML page or a text file that contain references to YouTube videos.

Now, let's imagine you are surfing the Net and then you come up to a page that contains a lot of YouTube videos you are interested in. Let's say the page you are visiting is a video container. Now what if you need to download them all?

I know there are a lot of YouTube video downloaders all around, but in situations like those described above, well... downloading each video separately can be just time-consuming. So here started my work.

The idea was to have a tool that, given a list of video containers, could display the basic info about each video found and then proceed to download. Therefore, I developed youtube-leecher, devoting some hours of spare time to it. I worked on it mainly during the night, from 10pm to 2am, when a lot of silence boosts my focusing. After about a week, the first promising results came out.

It was an interesting experience for some reasons I'm going to tell you: first off, Ruby. Ruby is a concise and powerful language. It's funny to solve problems using it. It gives you... excitement.
Another reason that caught me was I discovered the way to download a YouTube video, given its ID. (If you don't know it, downloading a YouTube video is not just a matter of right-clicking on it and choosing the option "save as", like for an image).
Finally, I found the way to query YouTube using Data API to obtain basic info about a video. Now it's the time to share these info with you.

Let's start from downloading a YouTube video.

Just suppose you want to download a video whose ID is video_id. Firstly, you need to access to the web page that allows you to watch the video. This page corresponds to URL:
http://www.youtube.com/watch?v=video_id
Then you have to look inside the Javascript code of that page searching for the definition of object swfArgs (var swfArgs = ...). As you will see, this object has several members. The most important for our purposes are: l, sk, fmt_map, t. Take notice of the value assigned to each of them inside the Javascript object definition statement. (In the following, we will refer to them in boldface). To download the video, all you need to do is calling a URL composed this way:
http://www.youtube.com/get_video?video_id=video_id&l=l&sk=sk&fmt_map=fmt_map&t=t
If a parameter value is set to null, just don't express the corresponding pair parameter name=parameter value inside the URL: so, if for instance fmt_map is null, don't write the part fmt_map=fmt_map.

Note that the aforementioned URL allows you to get a video in flv (Flash) format. Sometimes the mp4 version of it is available too. Currently the program doesn't allow you to download videos in this format, but just for your information, to get a video as mp4 (when available) just append to URL the parameter fmt=18.

What about getting info on a specified YouTube video?

It’s very easy: given a video whose id is
video_id, just call the following URL:
http://gdata.youtube.com/feeds/api/videos?vq=video_id

As the reference documentation for Data API says, this URL will provide you with a RSS that contains all the info about videos that satisfy the query condition (in our case just a video: the one with with id = video_id)

Beware of videos whose ids start with the character – (dash). Calling the previous URL as stated before doesn’t work. To have info about these videos, you have to call the Data API URL without providing the first character (the dash).

Now... what's about the program?

Let's imagine you have downloaded it right now and you want to get all YouTube videos from http://www.xy.com. All you need to do is writing:
youtube-leecher http://www.xy.com
Of course, you can specify several options, such as the directory where to place downloaded videos, or the video naming, just to name a few.

... I don't want to bore you anymore. If you are interested in, you'll find all these info inside the full package.

Tuesday, September 30, 2008

puts "Hello world"

Hello everybody from the magical world of Ruby: that's at least the meaning of the title...

Guess what? I'm learning Ruby in my spare time and I'm having a lot of fun. Well, I find this language concise, fascinating and powerful. So I strongly suggest every programmer that codes for fun (just like me) to give it a try. See the official website for more information.

For those of you who are guessing why I came to Ruby, I have two answers. The first is funny: after watching Giles Bowkett's fantastic presentation on his project Archeopterix, I was curious about the language he used for that project. The second is more serious: I'd like to experiment all the power of Ruby on Rails, a framework for developing web applications quicky. (As you probably know, I come from J2EE). So, before learning Rails, I need to understand Ruby.

Yesterday night I was trying to do a program in Ruby that given a URL or a file finds all the Youtube videos referenced inside it. After getting these information, I then added some logic to download them automatically. BTW I learned a lot of things about downloading a video from Youtube, given his ID. Also I used Youtube Data API in order to get some info about each video.

I will post it soon. Stay tuned!