ruby

Getting user information using either user.login or user.id

So I have a url like the following localhost/users/:id/posts which gives the posts of that particular user. Now this id can be either his login (which is a string) or the id (user.id) which is technically an Integer but params[:id] is always a string. So how do I implement this an action. @user = params[:id].is_a?(String) ? User.find...

Ruby on Rails: Length of Video as a Paperclip Attachment

I created a rails class with a video attachment, and i want to know how to get the length of a video that is uploaded to my application. How can I achieve that ? ...

Ruby on Rails: How do I detect a specific mongrel instance in a cluster during startup?

We run an application with a half dozen mongrels. A new feature that we've added is a scheduler (rufus-scheduler) that runs within a mongrel and provides cron-like background task processing. We want ot run this scheduler on only one of our mongrels but we can't figure out how -- during startup (environment.rb) -- to identify the speci...

Puzzled by ror Mechanize

I'm trying to use mechanize to perform a simple search on my college's class schedule db. The following code returns nil, however it works logging into facebook and searching google (with diff url/params). What am I doing wrong? I'm following the latest (great) railscast here. Mechanize documentation has been useful but I'm still puzzle...

Part II: How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together

This question is a continuation of my last one, regarding How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together. I've got that working now, but I'm still struggling to go the other direction. The PHP generated cryptogram appears to have all the information that was provided, but I cannot get the Ruby code to decry...

ruby on rails undefined method(s) for active record ..

(in /Users/sayedgamal/apps/test) /Users/sayedgamal/apps/test/config/boot.rb:20:Warning: Gem::SourceIndex#search support for String patterns is deprecated == CreatePeople: migrating ==================================================== -- create_table(:people) rake aborted! undefined method `string' for #<ActiveRecord::ConnectionAdapters::...

Best practice of installing Ruby and Rails on Snow Leopard?

I am setting up a new Macbook as a rails development machine. Till now, I always installed ruby, MySQL, etc. through MacPorts, because I don't want to be dependent on the system version of ruby. But with stuff like Homebrew and rvm (no link because of spam prevention) popping up, is there a better recommended way of setting up ruby for r...

Ruby - Socks4 proxy with WWW::Mechanize and NET::HTTP::GET

Hey, I searched on google and read in the ruby manuals, but I couldn't find a way to use WWW::Mechanize and NET::HTTP::GET over a socks4 proxy. I read, that WWW::Mechanize is a subclass of the UserAgent module, and that therefore the ->proxy() method would work. But the manual only talks about http, ftp and gopher proxy. Any ideas how ...

Using passphrase callback in ruby gpgme

I am using ruby gpgme gem (1.0.8). My passphrase callback isn't called: def passfunc(*args) fd = args.last io = IO.for_fd(fd, 'w') io.puts "mypassphrase" io.flush end opts = { :passphrase_callback => method(:passfunc) } GPGME.decrypt(input,output, opts) Does someone have working example of passphrase callback? ...

Make image transparent in ruby

Greetings, I was wondering if anyone could suggest to me potential libraries in ruby for manipulating jpegs? Specifically I want to make the image 50% transparent so it can act as an overlay. The image in question would be of a house and I'd like it to overlay a village image. Just being specific about the image as the transforms may ...

How to create a method like ".find_by_something_and_something_else" using Ruby?

Using Ruby I know you can get pretty creative with how you name your methods. For instance in rails you have .find_by_this_and_that. How can I do this? Example: def get_persons_with_5_things res = [] persons.each do |person| if person.number_of_things == %MAGICALLY GET THE NUMBER 5 FROM FUNCTION NAME% res << person ...

Why does ruby output values like this?

Fairly new to ruby, can someone explain why these 2 things respond differently? a=["A","B","C"] puts a A B C puts "#{a}" ABC a.to_s returns the same output as the templating output, but shouldn't the simple "puts a" do the same? ...

What is the best way to keep database data encrypted with user passwords?

Let's say an application has really specific data which belongs to a user, and nobody is supposed to see it except the owner. I use MySQL database with DataMapper ORM mapper. The application is written in Ruby on Sinatra. Application behavior: User signs up for an account. Creates username and password. Logs into his dashboard. Some ...

Nokogiri and random div name

Using Nokogiri and Ruby. I have a page to parse with div id's like: div id="some-list-number^875" Numbers after ...-number^ changes random, and i just can't do doc.css('#wikid-list-genres^875').each do |n| puts n.text.to_s end But the base structure is always the same -number^..some digits... So i need some kind of wildm...

ruby regular expression and extraction from string

I've the following string. How can I extract out the "somesite.com/2009/10/monit-on-ubuntu/" part from it using ruby regular expression? http://linkto.com/to/1pyTZl/somesite.com/2009/10/monit-on-ubuntu/t The common is, starts with "/to/some-alpha-num" and always ends with "/t" ...

The Ruby community values simplicity...what's your argument for simplifying a db schema in a new project?

I'm working on a project with developers who have not worked with Ruby OR Rails before. They have created a schema that is too complicated, in my opinion. The schema has 117 tables, and obtaining the simplest piece of information would require traversing/joining 7 tabels...and of course, there's no "main" table that serves as a sort of...

Question about a/bingo basics (a/b testing)

from: http://www.bingocardcreator.com/abingo/usage #A view example with a block passed to ab_test: <% ab_test("call_to_action", %w{button1.jpg button2.jpg}) do |button| > <%= image_tag(button, :alt => "Call to action!" %> <% end %> Does whatever "choice" that gets passed in the block have to be some sort of link? How does a/bingo kn...

Can't get gravity to work with RMagick and 'caption'

I'm using RMagick 2.12.2 with ImageMagick 6.5.6-10 on Snow Leopard. I'm trying to put captions on a collection of photos, and I'm getting the caption to work (i.e. it appears on the image), but I can't get the gravity parameter to work correctly. No matter what I set it to, I end up with some variation on NorthGravity. For instance: Se...

ActiveRecord :select is messing with my data types

So I have this very simple snippet. Topic.all(:select => 'count(*) as cnt')[0].cnt # behaves the same on all models "500" # What, a string? It seems that for some reason ActiveRecord is coercing the count to a string. In fact, I notice it coerces everything in the select list missing from the original object to a string. Why does...

Is there a Ruby version of for-loop similar to the one on Java/C++ ?

Is there a Ruby version of for-loop similar to the one in Java/C(++)? In Java: for (int i=0; i<1000; i++) { // do stuff } The reason is because I need to do different operations based on the index of the iteration. Looks like Ruby only has a for-each loop? Am I correct? ...