ruby

How do I get elapsed time in milliseconds in Ruby?

If I have a Time object got from : Time.now and later I instantiate another object with that same line, how can I see how many milliseconds have passed? The second object may be created that same minute, over the next minutes or even hours. ...

Reading previous line of file with Ruby

How to read the prevous line of a file. The opposite of IO.gets.I initially thought to set IO.lineno to the line number I wanted to read but that doesn't work as expect. How do you actually read the previous line? ...

Thinking Sphinx: Missing Attribute for Foreign Key

Hi, I know a very, very similar question has been asked before. The hackish solution to that question doesn't work if I want to chain in more scopes, so I'm asking again here, with a bit more information as to where the issue is coming from. # Relevant code only... class Publication < ActiveRecord::Base has_many :issues has_many :a...

Reading previous line of file with Ruby

Using Ruby, I am reading a file line by line, using IO.gets to incrementally read the next line of the file. Under certain circumstances I want to do the opposite (look at the previous line by decrementing). The way I tried to accomplish this was... IO.lineno = int IO.gets It seems that no matter what I set "lineno" to equal it still ...

Find type of the content (number, date, time, string etc.) inside a string

Hi, I'm trying to parse a CSV file and automatically create a table for it using SQL commands. The first line in the CSV gives the column headers. But I need to infer the column type for each one. Is there any function in Ruby that would find the type of the content in each field. For example, the CSV line: "12012", "Test", "1233.22",...

Best way to count words in a string in Ruby?

Is there anything better than string.scan(/(\w|-)+/).size (the - is so, e.g., "one-way street" counts as 2 words instead of 3)? ...

Step through Ruby code at runtime on OS X

I would like to step through Ruby code at runtime, but it seems difficult to find a decent Ruby IDE for OS X which allows you to do this. I would prefer one which uses the native look and feel of the OS (Cocoa). Can anyone make a suggestion? The main feature I am looking for is the ability to step through the code, not really much else...

To Ruby or not to Ruby

I know that this is a difficult question to answer, but I thought I would try anwyays.... I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. It is also currently using Ruby on Rails -- but mostly just for the...

Trying to understand a piece of code from Innate

Please consider the following context from Innate: # Default application for Innate def innate(app = Innate::DynaMap, options = Innate.options) roots, publics = options[:roots], options[:publics] joined = roots.map{|root| publics.map{|public| ::File.join(root, public)}} apps = joined.flatten.map{|pr| Rack::File.new(pr) } app...

Why is the splat used inside an array definition here?

def initialize(apps, catch=404) @apps = []; @has_app = {} apps.each { |app| add app } @catch = {} [*catch].each { |status| @catch[status] = true } end In this method from Rack::Cascade, what purpose does the splat(*) serve in the [*catch] code? I thought a splat was used in method arguments to indicate when you are going to ...

Why use lambda here instead of two predefined methods?

def divideset(rows, column, value) split_function = nil if value.is_a?(Fixnum) || value.is_a?(Float) split_function = lambda{|row| row[column] >= value} else split_function = lambda{|row| row[column] == value} end set1 = rows.select{|row| split_function.call(row)} set2 = rows.reject{|row| split_function.call(row)} [set1, set2] end ...

Use include? instead of a tracker

def add app @has_app[app] = true @apps << app end In the code above, instead of using @has_app[app] = true to keep track of the presence of 'app', couldn't we also say: @apps.include? (app) and do away with @has_app? I'm trying to understand why this separate variable is needed here at all (?). ...

Why is klass used in conjunction with const_get here?

def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj = Object klass.split("::").each { |x| obj = obj.const_get(x) } obj else try_require('rack/handler', server) const_get(server) end end In the code above, const_get is being used to retrieve some kind of named co...

How do I control REXML formatting on added elements?

I am a adding a couple of simple elements to a bunch of XML files (plists). The existing XML element I am working on looks like this: <dict> <key>background</key> <string>#FFFFFF</string> <key>caret</key> <string>#000000</string> <key>foreground</key> <string>#000000</string> <key>invisibles</key> <string>#BFBFBF</string...

Why is klass needed in this code?

def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact klasses.each do |klass| fname = output_filename(klass) FileUtils.mkdir_p(File.dirname(fname)) File.open(fname, 'wb') do |file| klass.new(@dat...

Best way to analyse data using ruby

I would like to analyse data in my database to find out how many times certain words appear. Ideally I would like a list of the top 20 words used in a particular column. What would be the easiest way of going about this. ...

Should I be using a framework for Ruby?

I have once again fleshed out Ruby, after two years of not touching it, and am considering programming for the web with Ruby. However, I have found that the Ruby on Rails framework is just too large and bloated for my taste. It is usually a matter of preference, but in my case, I just want to be able to program on the web without having ...

Which Ruby classes support .clone ?

Ruby defines #clone in Object. To my suprise, some classes raise Exceptions when calling it. I found NilClass, TrueClass, FalseClass, Fixnum having this behaviour. 1) Does a complete list of classes (at least core-classes) exist, which do not allow #clone ? Or is there a way to detect if a specific class supports #clone ? 2) Wtf? What ...

Is there a .NET framework similar to Ruby's Sinatra?

Does anyone know if there is a .NET library/API similar to Ruby's Sinatra? Just wondering since with the new Routing API in ASP.NET MVC, WCF and .NET 3.5, it seems like a possibility. ...

How to void authorized transaction in authorize.net gateway using ActiveMerchant

Goal: Only have successful purchases show up on a customer's billing statement. I don't want declined authorizations showing up on their billing statement (as seen in an online banking system) as pending. A customer often will accidentally input an incorrect billing address, for example, followed by a correct one. Together, the two at...