[1, 2, 3, 4].inject(0) { |result, element| result + element } # => 10
I'm looking at this code but my brain is not registering how the number 10 can become the result. Would someone mind explaining what's happening here?
...
Is there a module for Ruby that makes it easy to share objects between multiple processes? I'm looking for something similar to Python's multiprocessing, which supports process-safe queues and pipes that can be shared between processes.
...
Given my existing XML (test.xml):
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
</element>
</root>
And my ruby code:
require 'rubygems'
require 'xml'
parser = XML::Parser.file("test.xml")
doc = parser.parse
target = doc.find('/*/element')
target << child = XML::Node.new('child')
child['id'] = '...
I want to write a ruby script to recursively copy a directory structure, but exclude certain file types. So, given the following directory structure:
folder1
folder2
file1.txt
file2.txt
file3.cs
file4.html
folder2
folder3
file4.dll
I want to copy this structure, but exlcude .txt and .cs files.
So, the resulti...
Ubuntu -> Apache -> Phusion Passenger -> Rails 2.3
The main part of my site reacts to your clicks. So, if you click on a link, it will send you on to the destination, and instantly regenerate your page.
But, if you hit the back button, you don't see the new page. Unfortunately, it's not showing up without a manual refresh; it appears ...
Hi All,
A common idiom that my camp uses in rails is as follows:
def right_things(all_things, value)
things = []
for thing in all_things
things << thing if thing.attribute == value
end
return things
end
how can I make this better/faster/stronger?
thx
-C
...
Hello
I am trying to figure out how I can filter out key and value pairs from one filter into another
For example I want to take this hash
x = { "one" => "one", "two" => "two", "three" => "three"}
y = x.some_function
y = { "one" => "one", "two" => "two"}
Thanks for your help
...
In fairly large Ruby application, we have a situation where a given object is identified by a couple of things: name and id, say. Each of these value types serves a somewhat different purpose and so are not exactly equivalent (id and name persist in different places). So we wind-up with a variety of values being passed around the applica...
I have this string:
%{Children^10 Health "sanitation management"^5}
And I want to convert it to tokenize this into an array of hashes:
[{:keywords=>"children", :boost=>10}, {:keywords=>"health", :boost=>nil}, {:keywords=>"sanitation management", :boost=>5}]
I'm aware of StringScanner and the Syntax gem (http://syntax.rubyforge.org/) ...
What is the best way to write unit test for code which gets current time? For example some object might be created only at business days, other objects take into account current time when checking permissions to execute some actions, etc.
I guess that I should mock up the Date.today and and Time.now. Is this right approach?
Update: Bot...
I have a stories text field and want to show the first few lines say the first 50 words of that field in a snapshot page.In ruby(on rails) How do i do that??
...
I am building an application plugin in Python which allows users to arbitrarily extend the application with simple scripts (working under Mac OS X). Executing Python scripts is easy, but some users are more comfortable with languages like Ruby.
From what I've read, I can easily execute Ruby scripts (or other arbitrary shell scripts) us...
This code works in irb:
irb(main):037:0> eval <<-EOS
irb(main):038:0" #{attribute} = "host"
irb(main):039:0" puts machine
irb(main):040:0" EOS
host
=> nil
irb(main):041:0> puts machine
host
=> nil
irb(main):042:0> puts attribute
machine
=> nil
irb(main):043:0>
however when I try to execute the same code as a ruby script I g...
This should be simple, but I can't seem to find an easy answer.
How can I pass param values from the current request into a redirect_to call? I have some form values I'd like to pass into the query string of a GET redirect
I'd like to do something like:
redirect_to @thing, :foo => params[:foo]
and get sent to:
http://things/4?[foo...
method_missing
*obj.method_missing( symbol h , *args i ) → other_obj*
Invoked by Ruby when obj is sent a
message it cannot handle. symbol is
the symbol for the method called, and
args are any arguments that were
passed to it. The example below
creates a class Roman, which responds
to methods with names consisting of
r...
The following...
require 'yaml'
test = "I'm a b&d string"
File.open('test.yaml', 'w') do |out|
out.write(test.to_yaml)
end
...outputs ...
--- this is a b&d string
How can I get it to output
--- 'this is a b&d string'
???
...
class Tree
def initialize*d;@d,=d;end
def to_s;@l||@r?",>":@d;end
def total;(@d.is_a?(Numeric)?@d:0)+(@[email protected]: 0)+(@[email protected]: 0);end
def insert d
alias g instance_variable_get
p=lambda{|s,o|d.to_s.send(o,@d.to_s)&&
(g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))}
@d?p[:@l,:]:@d=d
end
end
...
I have a bunch of numbers I want to use to generate a histogram for a standard score.
Therefore I compute the mean and the standard deviation of the numbers and normalize each x with this formula
x' = (x-mean)/std_dev
The result is a number between -4 and 4. I want to chart that result. I am looking for a way to group the numbers in o...
Are there any good options other than the JVM for packaging Python or Ruby applications for distribution to end-users? Specifically, I'm looking for ways to be able to write and test a web-based application written in either Ruby or Python, complete with a back-end database, that I can then wrap up in a convenient set of platform-indepe...
SAP announced Blue Ruby, a version of Ruby that runs inside the ABAP Virtual Machine.
This seems to lend additional credibility to the Ruby language but, except for SAP developers, does this have any applicability to the rest of the Ruby community?
I'm just wondering what other significance this may have. Additional job opportunities,...