ruby

How to set Default format for activerecord fields of type string?

Here's an easy one: How do I go about setting the default format for a string field in ActiveRecord? I've tried the following: def phone_number_f "...#{phone_number}format_here..." end But I'd like to keep the method name the same as the field name. ...

Automatically require a class when running / starting ruby.

I am doing some monkey patching in some of Ruby classes and I would like that to be included AUTOMATICALLY whenever I run ruby. For example: I added the method trim to String. I want to be able to do this: ruby -e 'puts " aaaa ".trim' I don't want to do this: ruby -e 'require "monkey.rb"; puts " aaaa ".trim' Is there anyway to ...

Converting Unformatted String Of Objects Into An Array

I simply want to convert a string like this: str = "tree dog orange music apple" Into an array like this: arr = ["tree", "dog", "orange", "music", "apple"] I tried going down a path like this before realizing it's a dead end: str = "tree dog orange music apple" # => "tree dog orange music apple" str.gsub!(" ", ", ") # => "tree, do...

How do I modify an array while I am iterating over it in Ruby?

I'm just learning Ruby so apologies if this is too newbie for around here, but I can't work this out from the pickaxe book (probably just not reading carefully enough). Anyway, if I have an array like so arr = [1,2,3,4,5] and I want to, say, multiply each value in the array by 3, I have worked out that doing the following arr.each {|...

Array#split behaviour when accessing beyond the array boundry.

An irb session is following: >> ar = [:peanute, :butter, :and, :jelly] => [:peanute, :butter, :and, :jelly] >> ar[0, 1] => [:peanute] >> ar[4, 1] => [] >> ar[5, 1] => nil Why ar[4, 1] does not return nil just like ar[5, 1]? Would someone explain this behaviour? ...

How do I convert coordinates to google friendly coordinates

I need to do this.. <coordinates_east>6'01.4</coordinates_east> <coordinates_north>45'05.5</coordinates_north> I need to convert to this google friendly format in Ruby!...please note that these are not the real converted numbers just an example of the format I think I need! <coordinates_east>45.46998</coordinates_east> <coordinates_n...

Method definition without a name in Ruby on Rails

I recently looked at sample code for a controller in a rails project which included a method definition in a class without defining the name of the method like so: def begin redirect_to :action => :buy, :PaymentAction => params[:paymentaction] rescue Errno::ENOENT => exception flash[:error] = exception redirect_to :controller => '...

Converting degree/minutes/seconds to decimal degrees

I'm asking this question in a different way, another user let me know what I need to ask for, I'm basically looking to convert to "decimal degrees". The conversion of course must be accurate. I see javascript solutions out there like this javascript example. I need ruby. Maybe someone could write the javascript in Ruby and I can try that...

For what kind of Applications Ruby or Groovy are better choice ?

What kind of Applications are suited mostly for Ruby or Groovy ? Alternatively what problems are better addressed by programming languages like Ruby or Groovy ? ...

Ruby on Rails Internationalization

Hey, I try to make my config/locals organized in directories like the app directory. I can not get this to work... Here http://guides.rubyonrails.org/i18n.html says you have to activate this manually under config like this config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '**', '*.{rb,yml}')] but when I try to us...

How can I show updates with gem?

Hi I want on my webserver a script that checks automatical over cron if there are gem updates without installing updates. I don't find anything about this task in the gem docs. ...

Ruby charting library?

I need to display some charts/graphs based upon user inputs on my web aplication built using Ruby on Rails. Are there any charting libraries out there that I could use with ROR for displaying simple bar, line and pie graphs? ...

Get each value and position from a Text field to match ruby table_id

I have product that the product serial number equals product series, chip, model, manufacture, software, etc. Each of these has its own table with all the information I need in it. Each product serial number is 8 characters long. i.e. 1B43A672, 18GH8843. The second value determines how to map the serial number to which table. i.e. If ...

How can my Ruby "Time.now" timings be so low when my "ping" timings are so high?

I'm using MongoDB for the first time and trying to time its performance. I'm running ruby on a VirtualBox Ubuntu 9.10 guest with a Windows 7 64-bit host. MongoDB is on a remote host, not on my lan buit somewhere in the internet cloud. Here's my code: time1 = Time.now rows = coll.find(some_criteria) puts ((Time.now - time1) * 1000)....

Adding Variables to AR Object

How can I add a variable to a set of objects returned by ActiveRecord? I've looked around and none of the methods I've seen seem to work. Thanks in advance! ...

Signup form using Braintree Transparent Redirect

Hi, I'm developing an application in Rails and want the user to be able to signup and provide their card details on one form. I'm using the Braintree API and their transparent redirect, which means that the form data is posted directly to Braintree. How can I store and later retrieve the non-payment related information provided by the ...

functional test for Rails plugin, assert_template broken?

I'm writing a plugin that adds a class method to ActionController::Base, so in my functional test I am creating a simple controller to test against. The example below is totally stripped down to basically do nothing. The test succeeds all the way up to assert_template, which fails. require 'rubygems' require 'test/unit' require 'active_...

Ruby Support for SVG

Is there some library or helper to produce SVG graphics with Ruby? I googled a bit and found many, but all seem to be dusty and very incomplete… Does anyone know about some nice, reliable alternative? ...

Where did the div class=fieldWithError go?

I have the following simple form: <% form_for(@weight) do |f| %> <%= f.error_messages %> <%= f.label :weight %>: <%= f.text_field :weight, :size => 5 %> kg. <%= f.submit "Add weight" %> <%= f.error_message_on :weight %> <% end %> which displays a form of only one field: weight. Normally it renders like this: <form action="...

Total for Current Users Tags

I'm working on a RoR application that deals with users creating entries. These entries have a price, a boolean which signifies the entry as either an income or an expense, and via the acts_as_taggable_on plugin, tags. Users act_as_taggers, so I'm able to apply ownership to tags. I'm trying to find the users tag that has the highest sum ...