ruby

Most efficient way to get a subset of tagged objects from acts_as_taggable find_tagged_with?

For example, I have a named scope sfw_only in my Image model that returns images with nsfw == false. I'm also using acts_as_taggable_on_steroids - and I'm trying to think of the most efficient way to do something like the following. if !params[:tag].nil? if nsfw_mode @images = Image.find_tagged_with(params[:tag]) else @imag...

Extending uniq method

This is Ruby 1.8 Question: We all know how to use Array#uniq : [1,2,3,1].uniq #=> [1,2,3] However I'm wondering if we can monkey patch it in a way to work with complex objects. The current behavior is like this: [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq #=> [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}] The requeste...

Iterative Parsing XML

Just getting started with Ruby and have a newbie question re parsing XML. I'm trying REXML (yes, I know hpricot and others exist, but I am starting with REXML to learn.) The problem is how to iteratively proceed through a XML doc like this: <?xml version="1.0" ?> <bars> <bar> <id>29</id> <foo>Something</foo> </bar> ...

Is it possible to get HTML tag autocompletion in Netbeans IDE?

I just installed Netbeans 6.7 for Ruby and one of the things that bugs me is I have to type both the opening and closing HTML tags even though it seems to recognize that I have completed the opening tag. Is it possible to configure it to automatically type the closing HTML tag for you, as some other IDEs do? ...

Count number of nodes in a level in a nested set

I am using Ruby (Ruby on Rails) and have a nested set of about 2000 nodes. Each node can only have two children. What is the best way to determine how many nodes are in each level? Do I have to crawl the entire tree looking for sibling to do this? Thanks! ...

Diff two XML doc in Ruby?

What's the easiest way to see the difference between two xml files using? I looked into Hpricot and Nokogiri but couldn't find any good comparison methods. I've also looked into unix tools like diffxml, but would rather use something in ruby. Anybody got any ideas? ...

Radiant mailer and flash message

Hello, I'm using Radiant with the mailer extension to provide a contact form on my website. I'd like to display a nice "your email has been successfully sent" message after sending the email. However Radiant seems to only allow redirect in it's configuration. Not to define flash messages. Would you know of a way to define flash message...

Ruby on Apache with mod_ruby

Hello. I really want to run some ruby code on Apache server. I've got libapache2-mod-ruby and libapache-ruby1.8 installed (complete list of what is installed is here). What do I do now to make it run (here`s an example)? ...

Ruby: delete multiple hash keys

I often find myself writing this: params.delete(:controller) params.delete(:action) params.delete(:other_key) redirect_to my_path(params) The trail of deletes doesn't feel right and neither does: [:controller, :action, :other_key].each do |k| params.delete(k) end Is there anything simpler and cleaner? ...

How to specify daemon's log and pid directories?

Using Daemons, how do I specify that my script's log goes in /log/ and its pid goes in /tmp/pids/? I've read the docs, and I see :dir/:dir_mode, but I can only make it do one or the other, not both -- seems like a really bad set of options if you ask me. ...

SQL Search in Ruby

Ok, this seems like a duplicate question to this: http://stackoverflow.com/questions/1545893/sql-super-search but it's a different approach. Before I was looking for a lean and efficient way to do this entirely on the database side, but now I was wondering if anyone knoew how to do something like this in Ruby. I've tried this, and while...

Ruby: building a plot of function

What's the easiest way to build a plot of a function under Ruby? Any suggestions as to the special graphical library? update: under windows only :-( ...

Tracing a ruby process as it is running

Hi folks! I am running a ruby code that has a significant number of http (web and api) requests, as well as some pretty intensive data processing. As you might expect it is pretty slow, and i want to find out where the main bottle necks are. I do not think i need a full blown profiler (yet), but want some way of tracing the process as it...

prepopulating admin user in database with authlogic rails plugin

I've been using the Authlogic rails plugin. Really all I am using it for is to have one admin user who can edit the site. It's not a site where people sign up accounts. I'm going to end up making the create user method restricted by an already logged in user, but of course, when I clear the DB I can't create a user, so I have to prepopul...

Vim indenting file for Treetop (Ruby parser)

Has anyone seen a vim indent file for treetop, the Ruby parser/generator? I've found a vim syntax highlighting file, but haven't seen one for indentation. ...

Rails based S3 file manager

Hi, I'm looking for an open source project that provides a file manager type interface to S3. The ability to view files and "folders", add/edit/delete files/folders, etc. I've seen http://s3fm.com, but I'd like to host something like that myself. Does anything like this exist? Thanks. ...

How come when I run ruby.exe / IRB all I get is a blank DOS shell?

Hi, I installed ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32] on my Windows XP laptop. When I run ruby.exe I get a blank DOS Shell window. No expected "irb(main):001:0>" at the top left of the command prompt. I can type into the shell but, any code I type in actually does anything when pressing enter. I should mention tha...

How do I find the value of exponent in Ruby?

Consider the equation below: 2 ** n = A Let us assume A=64. What is the easiest way to find the value of n? I am currently using following two approaches A= 64; n = 1; n+=1 while (A >> n) > 0; n-1 A= 64; n = 0; n+=1 until (A == ( 2 ** n));n Is there a better approach? Other way of stating the same problem: 2 = nth root A If I k...

Static files served from CIFS Windows share through nginx/Ubuntu aren't fully sent to the browser.

We have a Rails app with nginx front end. Users can upload files, which are stored on a Windows network share, and retrieve them later. The uploaded files are served statically through nginx. Downloading an image, for example, will only download a portion of the original image. Refreshing the page successfully downloads a little more of...

using ruby popen wrapped in a shell script

I finished my short file for a homework assignment which uses IO.popen("command").readlines to grab the STDOUT of that command. However, I need to write a shell script to wrap my ruby file in. No problem, but somehow putting it in the shell script makes readlines hang. ruby script.rb foo example > example.out this works script.sh foo...