ruby

How reliable is JRuby?

I'm interested in promoting JRuby in our office as the platform hosting Rails applications. But how reliable is it? Is its performance better than MRI 1.8.7? JRuby 1.5 states that it is 100% compatible with Ruby 1.8.7, does this mean that JRuby can run any Ruby/Rails code? ...

Why are Crypto++ and Ruby generating slightly different SHA-1 hashes?

I'm using two different libraries to generate a SHA-1 hash for use in file validation - an older version of the Crypto++ library and the Digest::SHA1 class implemented by Ruby. While I've seen other instances of mismatched hashes caused by encoding differences, the two libraries are outputting hashes that are almost identical. For insta...

RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and "invalid byte sequence in US-ASCII"

I just started the upgrade process from Ruby 1.8.7 to Ruby 1.9.2 (using RVM). I have all my applications running using 'script/server' (or 'rails server') with 1.9.2, however, only Rails 3.0.0 RC applications work with Passenger. The error message given by Rails 2.3.8 applications is: invalid byte sequence in US-ASCII I'm guessing ...

Why Ruby's array of 1000 hashes' key and value pairs are always in a particular order?

Say if there is an array of 1000 hashes, with pairs like {:id => 1, :name => 'something', :created_at => '2010-08-18'} when I use a loop to print out those 1000 records, supposedly, the hash's key / value pair order is not guaranteed, but the print out of the table, it always appear in the same order. Why is it and can it be counted on...

Ruby URI Manipulation

Hello, Is there any way to manipulate a URI in Ruby to accept parameters without the use of ? and &? For example, I wish to do the following: http://localhost:5000/service/get/ip/port instead of http://localhost:5000/service?ip=192.168.1.1&port=1 To return information for a given device. This would utilizes a fully REST-based i...

How do I handle large amounts of logfile data for display in dynamic charts?

I have a lot of logfile data that I want to display dynamic graphs from, for basically arbitrary time periods, optionally filtered or aggregated by different columns (that I could pregenerate). I'm wondering about the best way to store the data in a database and access it for displaying charts, when: the time resolution should be varia...

Ruby CLI app configuration / argument management

I'm currently working on a CLI app in Ruby, I'm using Trollop (http://trollop.rubyforge.org/) for dealing with cli arguments. I'd also like to implement the possibility of storing the required options in ~/.mycfg as well as cwd/.mycfg, the latter taking precedence. The behaviour I'm trying to implement is: If .mycfg exists in the curr...

ruby hash storing

I want to take the current time and store it in hash and then when I check it again compare the current time to the stored time. t1 =Time.now time = "#{t1.hour}" + "#{t1.min}" + "#{t1.sec}" oldtime = Hash.new(time) puts "Old time is #{oldtime}" puts "Current is #{time}" Thanks for your help! ...

Character indices of a string containing unicode characters

I'm linkifying @mentions in status messages returned by Twitter's API. One of the tweets has a unicode character in it. Parsing the JSON (with either the json gem's JSON.parse or ActiveSupport::JSON.decode) returns a string that displays correctly, but the indices for the start and end of the @mention specified by the entity don't ma...

What does the % symbol do or mean in Ruby?

if counter % 2 == 1 I am trying to decode this line -its rails project and I am trying to figure out what the % does in this if statement. ...

Rails 2.3 Namespace and Nested Shallow Resources Prefix Issue

I've added two namespaces into my routes that split the public and admin areas respectively. The idea is that they share models but the views and controllers are different for each namespace. My admin namespace is /admin and my public namespace is just / So... map.namespace :admin, :path_prefix => "/admin", :name_prefix => "admin_" do...

Sinatra, select from Datamapper pass to Haml

I'm doing a brief exercise, condensed below. The issue I'm having is that I'm able to pass a selection of all tickets, but not a selection of one ticket. At / there is no problem listing all the tickets, at endpoint for a ticket I get: NoMethodError at /pi2l9ulnw undefined method `slug' for # I'm relatively new to Ruby and cutting and ...

Recursively Searching in a Tree

I'm working with the Stanford Parser in ruby and want to search for all nodes of a tree with a particular label name. This is the recursive method i have coded so far def searchTreeWithLabel(tree,lablename,listOfNodes) if tree.instance_of?(StanfordParser::Tree) if tree.lable.toString == lablename then listOfNodes << tree ...

Rspec is giving an error with my layout links from the rails tutorial: "Failure/Error: Unable to find matching line from backtrace"

Hi, I'm following the rails tutorial here: http://railstutorial.org/chapters/filling-in-the-layout#top When i run "rspec spec/", i get a bunch of errors that look like this: 1) LayoutLinks should have a Home page at '/' Failure/Error: Unable to find matching line from backtrace stack level too deep # C:/Ruby19/lib/ruby/1.9....

Collection Select in HAML fails to wrap correctly.

Does anyone know why %select{:name => "dropdown"} expand » - for say_text in @available_says %option = h say_text in HAML resolves to <select name='dropdown'></select> <option>a</option> <option>b</option> <option>c</option> in HTML? It's stymieing my dropdown completely and the documentation all says "That should work." I can't i...

Ruby on Rails: How do you explicitly define plural names and singular names in Rails?

For example, I'm using "Bonus" as my model, so I'd expect "bonuses" to be the plural form and "bonus" to be the singular form. However, in Ruby, this results in: "bonus".pluralize # bonus "bonuses".singularize # bonuse So, when I do a "has_many :bonuses", for example, it doesn't use the Bonus.rb model (since Ruby expects a Bonuse.rb ...

Check if one specific line exists in a file?

I want to add the following line to ~/.bashrc: export PATH:/var/lib/gems/1.9.1/bin:$PATH But only if it doesn't exist. How could I check whether that line already exists? ...

Another way instead of escaping regex patterns?

Usually when my regex patterns look like this: http://www.microsoft.com/ Then i have to escape it like this: string.match(/http:\/\/www\.microsoft\.com\//) Is there another way instead of escaping it like that? I want to be able to just use it like this http://www.microsoft.com, cause I don't want to escape all the special charact...

Ruby: Sorting an array that is "linked" to another array

In my rails app, I have a loop in my controller that does this: event_prices = [] event_dates = [] for event in @customer.events event_prices.push(event.total_prices) event_dates.push(event.event_time.strftime("%b %d, %Y at %I%p")) end I then use the arrays to push data into a highcharts graph. What I want to do is sort the event_...

Add a new line in file?

I want to add a new line after a string is inserted. My current code looks like this: File.open(filename, 'a') do |file| file.write @string end How could I add a new line after the string is inserted? ...