ruby

Ruby Gem Management

As a Ruby/Rails dabbler, one of the things that's long bothered me is this constant sense that I've just got gem clutter all over the place. Chalk it up to developer OCD, perhaps, or even my novice status as a Ruby/Rails dev, but not knowing where stuff is used, even if it's used, where it lives in my file system or whether it's part of ...

How to split between two capital letters?

I have the following array: a = ["CH3", "CH2"] and I'd like to split this between two capital letters using a reg expression to display: a= ["C", "H3", "C", "H2"] How do you do this? so far I've tried: a.each { |array| x = array.scan(/[A-Z]*/) puts a } returns: CH CH Thanks in advance! ...

Manually Retry Job in Delayed_job

Delayed::Job's auto-retry feature is great, but there's a job that I want to manually retry now. Is there a method I can call on the job itself like... Delayed::Job.all[0].perform or run, or something. I tried a few things, and combed the documentation, but couldn't figure out how to execute a manual retry of a job. ...

How to add more than one piece of data to an array follow the .each method

I have the following method: def speciate chem_formula.each { |chemical| @chem_species = chemical.scan(/[A-Z][^A-Z]*/) puts @chem_species } end that produces: H2 S O4 @chem_species = ["S", "O4"] from: @chem_formula = ["H2" "SO4"] How do you set the array to include all iterations? That is how do you output ["H2", "S"...

Rails: Don't save date if it wasn't changed

I have a class with a date. If the user doesn't change it in the view, Rails saves today's date (the default values in the drop downs). I would like it to be saved as a null value instead. How do I do this? ...

How to handle Timeouts in Ruby with TCPSocket networking

I want to be able to return quickly from a networking call that times out (having seen that my code can hang forever if the network is down, or similar) My code looks like: s = TCPSocket.open(hostname,port) s.puts msg ret = s.recv(1024) s.close I'm pretty confused at the documentation for TCPSocket, for example, doesn't even MENTION ...

Mongoid - getting all attributes including embedded documents

Is there an easy way to get all attributes of a Mongoid document, including those of embedded documents? For example, if I have the following documents: class Person include Mongoid::Document embeds_many :phone_numbers field :name end class PhoneNumner include Mongoid::Document embedded_in :person, :inverse_of => :phone_numb...

Why do I get an error trying to refer a nested class in Ruby?

Why there is an error in the following example? class ClassA class ClassB end class ClassC def test ClassB.new end end end p ClassA::ClassC.new.test # => #<ClassA::ClassB:0x0000010103f860> class ClassA class ClassD def test ClassB.new end end end p ClassA::ClassD.new.test # => #<ClassA::ClassB:...

Mounting multiple instances of the same engine in Rails 3

Hi, I'm wanting to make use of the new modularity prospects offered by the architecture changes in rails 3. However, rather than just mount an engine as in the majority of examples... Rails.application.routes.draw do match "/blog" => Rack::Blog end ...I'd like to mount multiple versions of the same engines, and have those instance...

Create rails locale yaml file automatically ?

hi is it possible to create / update a locale language yaml file from with a rails application ? If so is would it be automatically pulled into the load path somehow as i dont want to have to restart to pull in the new changes ? Is this possble and if so how ?? or is there a better way ? I am using mongodb as a db. thanks rick ...

Increasing the Loading Speed of Large Files

There are two large text files (Millions of lines) that my program uses. These files are parsed and loaded into hashes so that the data can be accessed quickly. The problem I face is that, currently, the parsing and loading is the slowest part of the program. Below is the code where this is done. database = extractDatabase(@type).chomp(...

Ruby undefined variables in views

I've got a view which looks like this: <p><%= stage.end_date.strftime("%A, %d %B %Y")%></p> Sometimes stage.end_date returns null and that's ok, but Ruby throws a NoMethodError. I'm quite new to Ruby so I want to know how I should I deal with missing/null varibles in views Do I need to test for stage.end_date in the view? because t...

Can't run Rails under Cygwin, due to LoadError: No such process from digest/md5

In trying to run Rails under Cygwin, the ./script/server command is producing a load error that I can't find a solution for searching on Google. I can reproduce this error in irb as follows: irb(main):001:0> require 'rubygems' => true irb(main):002:0> require 'digest/md5' LoadError: No such process - /usr/lib/ruby/1.8/i386-cygwin/digest...

If I define a method in Ruby, does it belong to any class?

I have feeling, that if one defines a method def test puts 'Hi' end then there is a class to which this method belongs to (i.e. Unknown#test). So one probably has a possibility to list all methods defined "outside" of other classes. Or there is another way to do such listing? ...

Error with overwriting Ruby class only manifests itself in production?

I recently ran into an odd error that only occurred in production mode. I was using the paths of glory gem that defines the class Achievement (http://github.com/paulca/paths_of_glory/blob/master/app/models/achievement.rb). In the base class, level is defined: def level(level, options = {}) levels << {:level => level, :quota => optio...

Does Ruby provide the namespace path, e.g. something like [:A,:B] for class A::B::C?

Is the following possible? puts A::B::C.new.namespace_path # => [:A,:B], or even [A,B] ...

How to get data to a running ruby process?

I have a datastore with a cache and a db, simple. The tricksy part is that I want a way to control if the the datastore hits the db in a real-time way. That is to say while the process is running I want to be able to toggle if it's connected to the db or not. I looked into env variables, but it doesn't seem like those get updated as t...

If I define a class-method in Ruby Object class, how do I get the name of a child class calling this method?

Example def Object.const_missing(name) puts self.class end class A; end A::B # => Class How can I get A in Object#const_missing? ...

GlassFish gem reload

Using JRuby from RVM and the GlassFish Ruby gem. I haven't been able to reproduce this in other projects. However, in one project of mine, regardless of what's in the config.ru, if I run $ glassfish, then it loads up the server and the output is from when I first launched GlassFish many days ago, even if I uninstall and reinstall the ge...

can't convert Array into Integer

I'm trying to iterate through an array, @chem_species = ["H2", "S", "O4"] and multiply a constant times the amount of constants present: H = 1.01 * 2, S = 32.1 * 1 and so on. The constants are of course defined within the class, before the instance method. The code I've constructed to do this does not function: def fw x = @chem_specie...