ruby

Colorized Ruby output

Is there a proper plugin or a class to perform background and foreground text colorization within a common output console? I remember, when programming Pascal we all used to play with textcolor(...) procedures to make our small educational programs look more pretty and presentational. Is there anything similar in Ruby? ...

Ruby on Rails: Access information from other models?

Another newbie Ruby on Rails question: In my post view, I want to show the authors name. The post database table stores the authors id which is the same as the users id column in the users table. The user also has a username column in the user table. So having the users id, how do I get the users name? ...

From Static Typing to Dynamic Typing

I have always worked on staticly typed languages (c/c++,java). For the poast months i have been playing with clojure and i really like it. One thing i am worried about is, say that i have a windows that takes 3 modules as arguments and along the way requirements change and i need to pass another module to the function. I just change the ...

How to get the number of days in a given month in Ruby, accounting for year?

I'm sure there's a good simple elegant one-liner in Ruby to give you the number of days in a given month, accounting for year, such as "February 1997". What is it? ...

Reading the first line of a file in Ruby

I want to read only the first line of a file using Ruby in the fastest, simplest, most idiomatic way possible. What's the best approach? (Specifically: I want to read the git commit UUID out of the REVISION file in my latest Capistrano-deployed Rails directory, and then output that to my tag. This will let me see at an http-glance what...

NoMethodError with collection_select

I'm building a form for a :has_many :through relationship: class Account < ActiveRecord::Base has_many :employments has_many :people, :through => :employments accepts_nested_attributes_for :employments end class Person < ActiveRecord::Base has_many :employments has_many :accounts, :through => :employments end class Employmen...

Modifying module level variables in an anonymous array in Ruby

I am in the midst of learning Ruby and thought I was clever with the following piece of code: [@start,@end].map!{ |time| time += operation == :add ? amount : -(amount) } where @start, @end are two module level variables, operation can be one of :add or :sub, and amount is an float amount to adjust both @start and @end by. Granted it ...

Comparing performance between ruby and python code

I have a memory and CPU intensive problem to solve and I need to benchmark the different solutions in ruby and python on different platforms. To do the benchmark, I need to measure the time taken and the memory occupied by objects (not the entire program, but a selected list of objects) in both python and ruby. Please recommend ways to...

Is there any software or library available to draw screws in 3 dimensions in C, C++, Java, or Ruby?

Is there any software or library available to draw screws in 3 dimensions in C, C++, Java, or Ruby? ...

Validate hash of multiple select inputs in Rails

In a similar vein to a previous question of mine I need to validate a group of select inputs for order of voting preference. This time I've had to change it slightly. The options to a user would be something like: Choose in order of preference 1: [select with options] 2: [select with options] 3: [select with options] The hash that t...

Validating a legacy table with ActiveRecord

Good day all, We are doing a data migration from one system to a Rails application. Some of the tables we are working with are very large and moving them over 1 record at a time using ActiveRecord takes far too long. Therefore we resorted to copying the table over in SQL and validating after the fact. The one-by-one validation check ...

In Ruby on Linux, how can I check for the existence of a named network interface like eth0?

In ruby 1.8.5, how can I check for the existence of a named network interface like eth0 or wan0 under linux? Even checking the existence would be a start. I'm aware that I could wrap a shell command to use ifconfig or somesuch, but would rather have a pure ruby solution. Another way of phrasing the question might be "If I was implemen...

How to get/set the console cursor position in Ruby (Windows)

I'm trying to write a shell in Ruby, and to implement tab completion I am using the WinAPI function getch to read in a character at a time from the user, checking for tabs. The problem with this is the backspace key: It moves the cursor further back than the prompt (eg, with prompt hello> , the user can backspace the cursor on to the...

YAML data sequence problem

Hello, I need to have correct order of values inside Ruby array after parsing YAML file. I have this simple example showing my issue: x = "columns:\n col_1 : ~\n col_2 : ~\n col_3 : ~\n col4 : ~" s = YAML::load(x) console output gives : x = "columns:\n col_1 : ~\n col_2 : ~\n col_3 : ~\n col4 : ~" => "columns:\n col_1...

Add readable field descriptions to ActiveRecord models

I'd like to add descriptions to ActiveRecord model fields to serve as basic instructions / examples for each of the fields. Basically model metadata. I can then display these in the UI (next to the fields on a form etc.) The way I'm planning to do it is simply create a static hashtable inside the model with the field name as the key and...

How do I dump a ruby on rails complex object to disk so that I can load that object into other data bases

I need to share an object (which has very complex relationships with other objects) between 2 or 3 completely separate sql databases. Ideally I'd like to have a script which is scripts/dump_object class_name object_id > file and script/load_object file. I've tried yaml_db (which dumps the whole db - which is not optimal) and it dies wi...

Multiple rubygems versions

Hi guys, Basically what I need is to install rubygems 1.3.5 on a machine (Debian) with 1.2.0 installed and having no root access. What I've done so far: installed rubygems into $HOME/rubygems and set up GEM_HOME + added bin to the path. So running "which gem" shows me the new binary, not the old one. Also when I gem install something, ...

How to automatically export oracle environment variable required to run a ruby script?

My ruby script requires connection to an Oracle database. So I need to export ORACLE_HOME and LD_LIBRARY_PATH correctly before the script would run. Is there a way that I can export those env variables without using shell script? I tried to put ENV['ORACLE_HOME'] = '/usr/local/oracle_client' at the first line of the script and it doesn't...

How can I handle (or prevent) SIGCHLD signals from ruby backquote invocations?

I have a long-running process with some child processes that must be restarted if they exit. To handle clean restarts of these child processes, I trap the exit signal with trap("CLD") do cpid = Process.wait ... handle cleanup ... end The long-running process occasionally needs to invoke 'curl' using a backquote as in `/usr/bin/cu...

comparing multiple strings character by character and outputting the overlap ?

ruby i have the following p = 0 [s1.size,s2.size].max.times { |c| if s1[c] == s2[c]; p = c; else break; end }; matched_part = s1[0..p] but i dont know how i can this for multiple strings (more than 2) at the same time. ...