ruby

When do we need to "require 'rubygems'"?

This is related to this question: http://stackoverflow.com/questions/3179797/how-to-use-rubytorrent-or-other-gems I thought RubyGems is a package manager, similar to apt-get on Ubuntu... So when do we need to require 'rubygems' in our code? ...

Ruby Unit test framework Question...Can i instantiate a test object from a normal ruby data object

Hi All I am trying to pass my test data to the test object. I have a data object which has the parameters/hash that i need to pass to the test object.I just need to use the test framework for 1) running assertions on data in my data object 2) and to be able show a cumulative results for a large number of these test objects through ...

Rails 3.0 & Ruby 1.9.2rc: Rake commands return 'already initialized constant' & stack level too deep errors. Any ideas

Hi Folks, I'm trying to run Rails 3 beta 4 & Ruby 1.9.2rc on Ubuntu 10.04. It worked initially, but after doing my first bundle install/package, I now get the following errors in all rails projects. Even a basic 'rails new testproject' followed by a rake brings up the error messages. In short, I'm stumped. Any help regarding what could...

Just starting in Rails --> Is it really buggy these days or is it just me?

Hi folks, I've spent a fair bit of time with PHP & Python frameworks and recently thought I'd branch out to rails. The framework itself I like, but I seem to spend at least half my development time navigating through odd bugs and/or version incompatibilities between rails/ruby/rake/gems. I'm happy to battle through it all if it gets le...

how to convert a ruby hash object to JSON

How to convert a ruby hash object to JSON? So I am trying this example below & it doesn't work? I was looking at the RubDoc and obviously Hash object doesn't have a to_json method. But I am reading blogs, that Rails supports active_record.to_json and it also supports hash#to_json. I can understand ActiveRecord is a Rails object, but Has...

Find by association (Datamapper)

I've got two models that look like this class Stage include DataMapper::Resource property :id, Serial belongs_to :staff end class Staff include DataMapper::Resource property :id, String, :key => true property :full_name, String property :email, String has n, :stages end I'm trying to find all Stages that hav...

Adding a helper method with a gem

I have found a lot of information about adding form helper methods (see one of my other questions) but I cant find anything about adding helper methods as if they where defined in application_helper.rb Ive tried basically copying application_helper.rb from a rails app into the gem but that didn't work. Ive also tried: class ActionVie...

Ruby String: how to insert tag every 5 characters

like this: s = 'HelloWorld-Hello guys' I want to inert tag every 5 characters the result I want like this: Hello<wbr>World<wbr>-Hell<wbr>o guys thanks! ...

how to find length of array class in ruby

$param=k.chomp.split("\t") how to find the lenght of $param thats is having class array. actully when i m writing puts $param.class i got the o/p like Array. now how to find the length of this array plzz help me i tried with $param.length but its not working ...

Getting a meaning of the string

I have the following string "\u3048\u3075\u3057\u3093". I got the string from a web page as part of returned data in JSONP. What is that? It looks like UTF8, but then should it look like "U+3048U+3075U+3057U+3093"? What's the meaning of the backslashes (\)? How can I convert it to a human-readable form? I'm looking to a solution with...

Starting a Ruby project: github + build tool

I am starting out to play around with Ruby and trying to make a desktop app using Ruby and wxRuby. So, to start out with a new ruby only project, I have few questions: I need a standard directory structure, what is the best possible way to do it? I tried out newgem with details here, but it seems that that the newgem website is not up...

Rails regex for validation of Google Maps latitude longitude

Hi guys, I'm trying to do a validates_format_of on the latlng object that is passed back from Google Maps API. I've got the map set up perfectly so that when I click on a point in the map it fills in a text-field with the latlng (which looks like this: 46.320615137905904, 9.400520324707031). I'm storing this value as a string in the db ...

Treat a string as binary in Ruby

I have a string coming from a database, for example 0b0101000. I'd like to cast it to a binary value, in order to apply byte operations on it, like 0b01011000 & (1<<0 | 1<<4) ...

How to replace the last occurence of a substring in ruby?

Hi guys, I want to replace the last occurence of a substring in ruby. What's the most eastest way? For example, in abc123abc123, I want to replace the last abc to ABC. How can I did that? ...

Embedded instance variables in Ruby 1.9?

Hi, the new object structure in 1.9 embeds some ivars into objects for faster access: #define ROBJECT_EMBED_LEN_MAX 3 struct RObject { struct RBasic basic; union { struct { long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; VALUE ary[ROBJECT_EMBED_LEN_MAX...

Using Ruby to find the first previous occurrence of a string

I'm creating some basic work assistance utilities using Ruby. I've hit a problem that I don't really need to solve, but curiosity has the best of me. What I would like to be able to do is search the contents of a file, starting from a particular line and find the first PREVIOUS occurrence of a string. For example, if I have the follow...

How to redirect output of Ruby programme to some file

I have the following code: puts "amit" puts "ravi" It outputs: amit ravi I would like to redirect amit to one file and ravi to a different file. Suppose my file name is name.rb. When I am trying like system("name.rb > #{@filename}") both amit and ravi are redirected to @filename but I want redirection to a different file or fi...

Why would a company decide to develop in Ruby on Linux and not on Windows?

Hi guys I would like to know if anyone meet a similar situation. What are the reasons for a company to decide that you must develop using Linux environment (virtual machine) and not Windows environment? Here there's no workstations with Linux at all - all computers have Windows installed. There's one production Linux server. Is it ...

How simplify this code? (is object belong to class)

How simplify this part of code: actor.inspect if actor.is_a? Array or actor.is_a? Hash I try sth like this, but it dosen't work proper: actor.inspect if [Array, Hash].each { |c| actor.is_a? c } ...

Installing Ruby Gems behind a Proxy

It appears this topic has been covered a few times before, but those solutions have only gotten me so far. I now have my sources properly updated and I am able to query for gems without an error but I keep getting empty results for my searches. I installed rubysspi and copied over the spa.rb file as mentioned in the readme. The readme...