I'm trying to redefine the File.dirname method to first change %20s to spaces. But the following gives me an error
class File
old_dirname = instance_method(:dirname)
define_method(:dirname) { |s|
s = s.gsub("%20"," ")
old_dirname.bind(self).call(s)
}
end
This trhows a NameError exception: undefined method ...
Hello,
I am writing some unit tests in ruby and have tests like the following:
def executing_a_signal
a_method(a_signal.new, a_model, a_helper);
assert_equal(new_state, a_model.state)
end
The tests work fine, but the method which runs just before the assertion to execute the logic prints various messages to the console (m...
There are lots of examples of how to strip HTML tags from a document using Ruby, Hpricot and Nokogiri have inner_text methods that remove all HTML for you easily and quickly.
What I am trying to do is the opposite, remove all the text from an HTML document, leaving just the tags and their attributes.
I considered looping through the do...
I am embedding ruby (1.9.1) as a scripting language in my window manager project (subtle). Since the switch from
1.8 to 1.9 I am facing many problems with asynchronous signals in multithreading.
How is signal handling supposed to work and does the thread that ruby creates automatically have any own
signal handlers? Due the asynchronous ...
My programming experience: I know Java, understand OOP and know some recursion.
I would like to learn how to write "scripts" for text processing, file handling and stuff like that. I'm fine with learning either Ruby or Python for this. I want a book that focuses on what I've mentioned above but most books just teach more than that (OOP,...
Lets say I have the following DataMapper resources:
class Post
include DataMapper::Resource
has n, :comments
...
end
class Comment
include DataMapper::Resource
belongs_to :post
...
end
To get the ordered list of posts, I know you can do:
@posts = Posts.all(:order => :date.desc)
But lets say I want to display al...
I have a Rails model that should only allow saving/updating of a model once per day per user. I have a callback to do the Find by user and date then add to errors but this is ugly and feels un-rails-like. I have the typical created_at / updated_at columns (and the time portion is significant/I need to keep it).
So I figure I could eithe...
What is the best way to build an XML tree in Ruby if you have an Array of string paths?
paths = [
"nodeA1",
"nodeA1/nodeB1/nodeC1",
"nodeA1/nodeB1/nodeC1/nodeD1/nodeE1",
"nodeA1/nodeB1/nodeC2",
"nodeA1/nodeB2/nodeC2",
"nodeA3/nodeB2/nodeC3"
]
xml =
<nodeA1>
<nodeB1>
<nodeC1>
<nodeD1>
<nodeE1/>
<...
I already removed all environment variables and ruby/ironruby directories and reinstalled it from scratch. And then I installed mocha through igem. Here are my outputs.
$ ir
IronRuby 0.9.1.0 on .NET 2.0.50727.3082
Copyright (c) Microsoft Corporation. All rights reserved.
>>> require 'mocha'
:0:in `require': no such file to load -- moch...
Hey there,
I've finally had a second to look into streaming, daemons, and cron
tasks and all the neat gems built around them! But I'm not clear on
how/when to use these things.
I have a few questions:
1) If I wanted to have a website that stayed constantly updated, realtime, with my Facebook friends' activity feeds, up-to-the-minute ...
What's the fastest/one-liner way to convert an array like this:
[1, 1, 1, 1, 2, 2, 3, 5, 5, 5, 8, 13, 21, 21, 21]
...into an array of objects like this:
[{1 => 4}, {2 => 2}, {3 => 1}, {5 => 3}, {8 => 1}, {13 => 1}, {21 => 3}]
...
I would like to populate various tables in my database after a new customer signs up to use our web application. I'm wondering what the best way to do this is, the main requirement being that it should not require any intervention by me. Most of the data is static (but can be changed by the customer later (like preferences for example)),...
Here is a clever trick to enable hash autovivification in ruby (taken from facets):
# File lib/core/facets/hash/autonew.rb, line 19
def self.autonew(*args)
leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
new(*args,&leet)
end
Although it works (of course), I find it really frustrating that I can't figure out how this...
I have just installed Ruby Enterprise Edition and would like to run some benchmarking tests against my system Ruby. Are there canonical benchmark tests I should implement?
...
So I'm putting together a simple forum. I'd like to allow my users limited formatting options and BBCode would be plenty for my users. Knowing that I'm assuredly not the first one to want to use BBCode with RoR I googled but couldn't find a straight forward tutorial on how to create a editor which accepts BBCode nor a way to parse and di...
Situation
# Models
class User < ActiveRecord::Base
has_many :items
end
class Items < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id
end
# Factories
Factory.define(:user) do |u|
u.name "foo"
end
Factory.define(:user_with_items, :parent => :user) do |u|
u.items {|items| [items.association(:item), ...
I've just started writing Ruby code from a C++ background. When I write the code, I find myself writing C++ code using the Ruby keywords(using while and for loops when a single Ruby command will make the code much shorter). Are there any sites which you use where people can look at code and recommend more optimal ways to use the langua...
Hi folks, i have a data set that ranges from 1 to 30,000
I want to normalize it, so that it becomes 0.1 to 10
What is the best method/function to do that?
Would greatly appreciate it if you could give some sample code!
...
Possible Duplicate:
Printing Reports and invoices with Ruby?
Hello,
Regarding to your topic : http://stackoverflow.com/questions/677183/printing-reports-and-invoices-with-ruby
Did you find a solution ? I want to do the same as you.
Thanks for advance.
Regards,
Ronnie Baret
...
Does giving a
idea = gets.reverse
print idea
If user inputted 'dog' it would come out 'dog'
But if you did this code...
idea = gets.reverse!
print idea
Then the string variable being returned would be 'god', right?
...