ruby

Finding palindromic numbers in Ruby

So, I'm doing Project Euler to solidify my Ruby skills. I'm on problem #4, which reads: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers. First, I'm trying to ver...

Does it make sense to create a Ruby gem that consists of only Rails template partials?

I'm trying to understand what does and doesn't work in Ruby gems (primarily from the perspective of creating one for myself to reuse functionality from one project to the next). When you create a Ruby gem, are you limited to including only Ruby functionality or could you create a gem that consisted of Rails templates, CSS files and Java...

Ruby YAML problem when deserializing a nested object structure

Hello guys, I have 3 classes that have the following relationship: Battlefield have an array of teams. BattleTeam have an array of members, and a reference to the Battlefield. Jaguar is a member of a BattleTeam and has a reference to it. If I serialize/deserialize Jaguar and up to BattleTeam, there is no problem. The problem happens...

ruby equivalent of c#'s internal keyword?

in C# an "internal" class or method can only be called from within the assembly that it is located... i know Ruby doesn't have "assemblies" or any kind of real package like them (other than gems, but they aren't really the same thing at all), but I'm wondering if there is a way for me to limit the location that a method can be called fro...

To use Model as Resource or not?

So I have a User, Post, and Vote models. User has_many Vote Post has_many Vote In my unit tests I was defining a method called @post.vote_up which creates a vote for a post, but then I started thinking whether or not such an interface would allow for restful methodology. If I were to call /topic/1/votes with a POST, the VoteControl...

Using Ruby and SCP/SSH, how to determine if a file exists before uploading a copy

I'm uploading a file to a remote server using SCP, but what's the proper way of seeing if that file exists before doing so? ...

Difference between or and || when setting variables

I was under the impression that || and or were synonymous. Setting variable with or does not hold value; why? >> test = nil or true => true >> test => nil >> test = false or true => true >> test => false Works 'as expected' with || >> test = nil || true => true >> test => true ...

writing to mysql table with ruby and generating absolutely unique id like youtube ?

how can i write to a mysql table with ruby ? run SQL. for example, if the ruby script finishes, write to mysql table saying its done and how can i generate an absolutely unique ID ? kinda like youtube's id. ...

How do I fill in a specific field in Webrat when several share the same name and id?

I'm just getting started with Cucumber and Webrat, and am adding feature specifications to an existing Rails application. One page in the app has multiple forms on it, representing different ways to build a new Character object. Here's a simplified example: <form id="adopt_character_form"> .... <input type="text" name="charact...

Set content-disposition on public file types in Rails?

I have some PDF files in a public rails folder. I want to set the response header "content-disposition" to "attachment". I know I can create a controller to read the files and set the header myself, but is there some general application wide setting that i can enable/configure? Thanks in advance. -JP ...

Ruby 1.9.1-p234, Passenger 2.2.5, Rails 2.3-stable closed stream on POST request

I've setup Ruby 1.9.1 (p234) on a Ubuntu server. I'm trying to deploy a Rails app which vendors Rails 2.3-stable on Apache 2.2/Passenger 2.2.5. GET requests work fine, POST requests break immediately with the following log entry: Processing UsersController#new (for 80.203.77.44 at 2009-10-24 20:54:55) [GET] Parameters: {"controller"=...

What's the most efficient way get the first day of the current month?

With ruby I'm trying to get format a date as such: 2009-10-01 Where I take the current date (2009-10-26) and then change the day to "01". I know of ways to do this, but was curious what the shortest way is, code wise, to pull this off. ...

Ruby's String#gsub, unicode, and non-word characters

As part of a larger series of operations, I'm trying to take tokenized chunks of a larger string and get rid of punctuation, non-word gobbledygook, etc. My initial attempt used String#gsub and the \W regexp character class, like so: my_str = "Hello," processed = my_str.gsub(/\W/,'') puts processed # => Hello Super, super, super simple...

Count and Select Object in ActiveRecord with 1 query

We have objects that we want to represent in stacks (think of stacking items in an MMO). There will be duplicate rows. Let's say our owned_objects table looks like this. user_id | object_id 1 | 27 1 | 27 3 | 46 3 | 46 5 | 59 I want the query to do SELECT user_id, object_id, count(*) AS count FROM ...

How do I setup my Rails Apps to Run through Phusion Passenger?

Hello folks, I have the following doubt: I have an application in Rails+MySQL and I want to run this with Apache + Passenger, I have both installed, but when I run ./script/server my app starts running with WebRick, how do I change it for work with Passenger and Apache? P.S: I'm on Ubuntu 9.04 Jaunty Jackalope, please consider the fact...

threads or multiple process ?

is it more efficient to run multiple instances of a ruby script ? or multiple threads within that single script ? take for example, a ruby script to check all links of a given domain are working. would you run multiple instance of this script or multiple threads ? how do you create multiple threads ? ...

Converting Ruby AES256 decrypt function to PHP.

I have the following function in Ruby that decrypts a bit of data: def decrypt(key, iv, cipher_hex) cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') cipher.decrypt cipher.key = key.gsub(/(..)/){|h| h.hex.chr} cipher.iv = iv.gsub(/(..)/){|h| h.hex.chr} decrypted_data = cipher.update(cipher_hex.gsub(/(..)/){|h| h....

using paperclip to get list of style/geometry pairs, even without valid object

I am working on maintaing an old code base and I'm migrating attachment_fu to paperclip. I migrated one thing but now I'm having a small issue. There's a partial that renders images given the type of image and a thumbnail style. I fixed the part to render the image and that's fine, but the "else" assumes that there actually is no photo ...

Perl & Ruby exchange AES encrypted information

What is the equivalent to Crypt::CBC in Perl for Ruby? Note: This problem similar to PHP/Perl at stackoverflow:655691. Perl Version use Crypt::CBC; use MIME::Base64::Perl; my $cipher = Crypt::CBC->new( -key => "95A8EE8E89979B9EFDCBC6EB9797528D", -keysize => 32, -cipher => "Crypt::OpenSSL::AES" ); $encypted = $ci...

Rails - Accessing Another Database?

I've set up a Rails app using MySQL, with the database 'DatabaseA'. My Rails app is a port of a PHP app that I've written earlier, so there's some data in the PHP app's database (DatabaseB) that I'd like to port over. It's not as simple as an export/import. I've made some schema changes to the database which would require me to manipulat...