ruby

Ruby Autovivification

I've been trying to use autovivification in ruby to do simple record consolidation on this: 2009-08-21|09:30:01|A1|EGLE|Eagle Bulk Shpg|BUY|6000|5.03 2009-08-21|09:30:35|A2|JOYG|Joy Global Inc|BUY|4000|39.76 2009-08-21|09:30:35|A2|LEAP|Leap Wireless|BUY|2100|16.36 2009-08-21|09:30:36|A1|AINV|Apollo Inv Cp|BUY|2300|9.15 2009-08-21|09:30:...

How do you specify a required switch (not argument) with Ruby OptionParser?

Say I'm writing a script and I want to require a --host switch (with value of course) but if the --host switch isn't specified I want the option parsing to fail. I can't seem to figure out how to do that. The docs seem to only specify how to make the argument value mandatory, not the switch itself. ...

Weird backslash substitution in Ruby

I don't understand this Ruby code: >> puts '\\ <- single backslash' # \ <- single backslash >> puts '\\ <- 2x a, because 2 backslashes get replaced'.sub(/\\/, 'aa') # aa <- 2x a, because two backslashes get replaced so far, all as expected. but if we search for 1 with /\\/, and replace with 2, encoded by '\\\\', why do we get this: ...

How can I call C++ functions from within ruby

Hi I am an experienced C/C++ developer but I am a novice in Ruby. How can I call a C++ function from with in Ruby? Sorry for the noob question. ...

How does one use rescue in Rails without the begin and end block.

I know of the standard technique of having a begin rescue end How does one just use the rescue block on its own. How does it work and how does it know which code is being monitored? ...

Ruby: If I wanted to convert order.price to a decimal value, do I use order.price.to_i?

Very beginner's question: I noticed that there's no .to_d (such as order.price.to_d to convert order.price into a decimal). Say order.price is currently a string; can I then use order.price.to_i to convert it into a decimal? ...

Testing modules in rspec

What are the best practices on testing modules in rspec? I have some modules that get included in few models and for now I simply have duplicate tests for each model (with few differences). Is there a way to DRY it up? ...

How can I ouput leading zeros in Ruby?

I'm outputting a set of numbered files from a Ruby script. The numbers come from incrementing a counter, but to make them sort nicely in the directory, I'd like to use leading zeros in the filenames. In other words, file_001... instead of file_1 Is there a simple way to add leading zeros when converting a number to a string? (I kno...

combination of a two array (ruby)

Hi , What is the ruby way to achieve following? Thanks. a = [1,2] b = [3,4] I want an array: => [f(1,3) ,f(1,4) , f(2,3) ,f(2,4)] EDIT: Sepp2k's way is very clever and impressive but Aaron used build-in function. I accept the build-in method as the answer. Thanks you all! ...

Best language for introductory "higher level" programming course.

My CS department is currently considering a new introductory "high level" programming course. It is partly aimed at non-majors or those with other primary majors that will find it useful to build programs, including biocomputation, etc. I have a PhD in programming languages, but I'm finding it hard to say exactly what languages current...

missing rails gem: zlib not found on snow leopard, but shows in ports

Trying to start development on a snow leopard machine. Have reinstalled and built ruby and ruby on rails via hivelogic post Prior to sorting out my own rails app, I created a test app, and all the expected files showed up. But when I try to run it I get Missing the Rails 2.2.2 gem. Please gem install -v=2.2.2 rails, update your RA...

Error: The requested service provider could not be loaded or initialized. - socket(2)

I am running a ruby script that uses Ruby/MySQL and net/ftp. The script is running on a Windows Vista box and is trying to create a database and ftp connection to the same remote Solaris server. Here is the gist of the code: require 'mysql' require 'net/ftp' dbh = Mysql.real_connect(db["host"], db["user"], db["pass"], db["name"]) ftp ...

Rails ActiveRecord fetch records with appropriate data types

Hi, I am using Ruby, I want data in array of hash format. I have tried out ActiveRecord's select_al method. But it returns all the data in string format, does not matter what type is its type in data base. All numbers are also returned in string format. Please, give your valuable suggestions. Thank you all for your precious time. T...

Large RoR project migration from Ruby 1.8.6 to 1.8.7+ solution

Hello! After updating to Snow Leopard, Ruby 1.8.7 (preinstalled with OS) become much more closer to me. I try to use port's but has no luck with this solution. (port ryby186 halt something without drop exception). So I start to think about update large scope of code to 1.8.7 requirements. Please help me with some scripts, articles and s...

Test::Unit tests passing on OS X, erroring out on CentOS

I have a set of Test::Unit tests for a Rails application. It was developed on OS X under Ruby 1.8.6, Rails 2.3.4. I'm also using thoughtbot-shoulda 2.10.2. I'm using standard Rails fixtures, not factories. I've checked out the project onto a CentOS Linux 5 workstation for another developer to work on. He's running Ruby 1.8.7. (The ap...

How to specify rubygems path in environment-less Ruby script?

I've written a data collection script for Cacti in Ruby and it runs fine from the command line but Cacti runs the script via "env -i" which strips the environment so Ruby can't find the rubygems library ("in `require': no such file to load -- rubygems (LoadError)"). How might I work around this? ...

How do I use Ruby 1.9 with Managed C++?

I'm trying to create a dot net library to use ruby, for the sake of this, I'm linking ruby's static library but I'm getting some LNK error. I'm using Visual Studio 2008, thanks in advance. ...

Can't get Ruby ODBC bindings to work in 1.8.6 compiled on snow leopard

I have been trying to get ruby 1.8.6 to connect to a MS SQL Server 2005 on Mac OS X snow leopard following the guide here http://wiki.rubyonrails.org/database-support/ms-sql. I have installed the Ruby ODBC Bindings and the dbi and dbd-odbc gems and ruby was compiled from source. If I run this in irb, DBI.connect('dbi:ODBC:A_DSN', '...

check for (the absence of) `puts` in RSpec

I am using rspec for my test in a ruby project, and I want to spec that my program should not output anything when the -q option is used. I tried: Kernel.should_not_receive :puts That did not result in a failed test when there was output to the console. How do I verify the absents of text output? ...

How to register a module to ruby?

How do I register a module to ruby with IronRuby and C#? ...