ruby

Ruby - pick randomly from an array

I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this: myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ] item = myArray[rand(myarray.length)] Is there something that is more readable / simple...

How can I refactor this simple Ruby snippet?

I have a class that looks like this: class Base < Library prelude "some-value" # from Library def foo; ...; end prelude "some-other-value" # from Library def bar; ...; end # ... others end I'd like to refactor it into something like the following: class Base < Library # what goes here to bring FooSupport and Bar...

How to declare a specific key for has_many association with :through option?

I have an association: an author has many books; and a book has many authors; I need to use :through option(through a table named 'relations',has two column named 'left_id' (used as author_id) and 'right_id' (used ad book_id); class Relation < ActiveRecord::Base belongs_to :books belongs_to :authors end class Author < ActiveReco...

is this a bad way of handling in Ruby

Hi, I want to monkey patch or extend enumerable. I want to handle nil cases as well and I have come up with the following test case and extensions: module Enumerable def has_elements (self) && (self.size > 0) end end class NilClass def has_elements false end end class EnumerableExtensionsTest < ActiveSupport::TestCas...

Radiant CMS database mailer extenstion

I am trying to use the Database mailer extension for my Radiant CMS application. I have followed the exact same steps mentioned here. http://blog.aissac.ro/radiant/database-mailer-extension/ When I try to run the rake task to migrate I get this error. I am fighting with this for a while now. Where and what am I missing? Radiant version -...

check number and remove leading zeros

I want to check a variable in ruby to see if it has two leading zeros (00) If it does the 00 should be removed How can this be done ...

Rails .megabytes method returns wrong value

Where is "megabytes" method for fixnums defined? As far as I understand, its not a core ruby method, since its only available in rails. ruby -e 'puts 5.megabytes' In default rails installation it returns: Loading development environment (Rails 2.3.8) >> 5.megabytes => 5242880 At some point in my app something wrong happen, and now ...

NoMethodError after loggin in in Ruby on Rails networking website

Hey everybody, I'm trying to make an example of a small networking site, and when I log in a user, it should redirect and show his profile, but I get the following error: NoMethodError in User#index Showing app/views/user/index.html.erb where line #5 raised: undefined method `screen_name' for nil:NilClass Extracted source (around li...

Not work. wget and block

Hello. I want to write a script which downloads all the podcasts from an rss-feed. This is code does not work: def wget @mp3_links.each do |m| system("wget", "#{m}") end end I understand that to be linked to the delay, but how? ...

Why are the gems not findable by 'gem list local' even though the gem folder is full of installed gems?

When I type: gem env on my Windows system, it produces this info: RubyGems Environment: - RUBYGEMS VERSION: 1.3.5 - RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32] - INSTALLATION DIRECTORY: C:/Ruby/lib/ruby/gems/1.8 - RUBY EXECUTABLE: C:/Ruby/bin/ruby.exe - EXECUTABLE DIRECTORY: C:/Ruby/bin - RUBYGEMS PLATF...

Paperclip Processors are not loaded in Rails 3.0.0rc

I just started using paperclip and have a custom processor. I have put the new processor in the correct location RAILS_ROOT/lib/paperclip_processors But, it does not get loaded. The reason for not loading, is that Rails.root is nil at the time paperclip is loaded. I've tested this by putting explicit code into the paperclip.rb puts "W...

Unicode characters in a Ruby script?

Hi, I would like to write a Ruby script which writes Japanese characters to the console. For example: puts "こんにちは・今日は" However, I get an exception when running it: jap.rb:1: Invalid char `\377' in expression jap.rb:1: Invalid char `\376' in expression Is it possible to do? I'm using Ruby 1.8.6. ...

How to use Capybara in pure Ruby (without Rails)?

I'm trying to get Capybara running in a simple Ruby script -- i.e. without/outside of Rails. Here's the script: require 'rubygems' require 'capybara' require 'capybara/dsl' include Capybara Capybara.current_driver = :selenium Capybara.app_host = 'http://www.google.com' visit('/') The problem is that when I run this I get this error...

in a rails migration, how can you remove the limit of a field

Is it something like: change_column :tablename, :fieldname, :limit => null ...

Decode a YAML serialized object

I have serialized an object in YAML and send it to a remote worker. The worker doesent have the object definition so i get a YAML::Object. How can i access the field inside it? A text field seems like that base64 encoded, how can i decode that? (no, decode64 not works). ...

Rails: Logging the entire stack trace of an exception

I have been trying to figure out the right way to log a stack trace. I came across this link which states that logger.error $!, $!.backtrace is the way to go but that does not work for me log_error does. As per documentation I do not see how passing a second argument to the error method would work anyway because the ruby logger that rail...

"Invalid multibyte char (US-ASCII)" error for ä, ü, ö, ß which are Ascii!

My application needs to handle some international characters, namely ä, ü, ö and ß, which are still ascii. When I tested the behavior of ruby when dealing with these chars, I got this error: test.rb:1: invalid multibyte char (US-ASCII) test.rb:1: invalid multibyte char (US-ASCII) for this code: puts "i like my chars: ä, ü, ö and ß!"...

How to split a HTML document using nokogiri?

Right now, splitting the HTML document to small pieces like this: (regular expression simplified - skipping header tag content and closing tag) document.at('body').inner_html.split(/<\s*h[2-6][^>]*>/i).collect do |fragment| Nokogiri::HTML(fragment) end Is there more easy way to perform that splitting? The document is very simple, j...

Which implementation of Ruby will survive?

From what I've heard there was IronRuby and Ruby.NET and a few other variations. I've also heard that Ruby may not be supported anymore by Microsoft as they started to lay of much of that development group. I'd like to know which implementation(s) of Ruby is likely to survive, and which one will be adopted into large enterprises. It w...

Cucumber can't find routes? This is potentially a big newb question.

Rails3 app with Rspec2 and Cucumber Cucumber Given /^that a user is logged in$/ do current_user = User.first render new_user_post_path(current_user) end Routes.rb map.resources :users do |users| users.resources :posts, :collection => {:view => :get} end posts_controller_spec describe PostsController do describe "#new" do ...