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...
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...
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...
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...
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 -...
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
...
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 ...
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...
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?
...
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...
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...
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.
...
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...
Is it something like:
change_column :tablename, :fieldname, :limit => null
...
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).
...
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...
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 ß!"...
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...
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...
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
...