ruby

Ultra-grand super acts_as_tree rails query

Right now I'm dealing with an issue regarding an intense acts_as_tree MySQL query via rails. The model I am querying is Foo. A Foo can belong to any one City, State or Country. My goal is to query Foos based on their location. My locations table is set up like so: I have a table in my database called locations I use a combination of ac...

Animating with Ruby

This relates to both physical programming as well as Ruby running on a web server. I have an array of RGB leds, which is 5x5 so a total of 25 leds. They are numbered and individually addressable as such: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Here is a photo: As for the hardwa...

How to iterate through an array starting from the last element? (Ruby)

I came with below solution but I believe that must be nicer one out there ... array = [ 'first','middle','last'] index = array.length array.length.times { index -= 1; puts array[index]} ...

Using a Regex in the URI of a Mongrel Handler

Hi all, I'm currently using Mongrel to develop a custom web application project. I would like Mongrel to use a defined Http Handler based on a regular expression. For example, everytime someone calls a url like http://test/bla1.js or http://test/bla2.js the same Http handler is called to manage the request. My code so far looks a like...

How to check if an object is nil in a view in Ruby?

I would like to display a line of text only if an object called @foo is set. In my view, I'm trying something like this: <% if [email protected]_record? || [email protected]? %> Foo is not a new record or nil <% end %> But this fails, returning You have a nil object when you didn't expect it! I'm pretty sure this happens because of the new_record? ...

How to get 'value' of select tag based on content of select tag, using Nokogiri

How would one get the contents of the 'value' attribute of a select tag, based on content of the select tag (i.e. the text wrapped by option), using Nokogiri? For example, given the following HTML: <select id="options" name="options"> <option value="1">First Option - 4</option> <option value="2">Second Option - 5</option> <op...

Does a mocked method's code actually run

Hi I'm using Mocha for a Rails project. I'm new to TDD so please forgive me if this is a stupid question. If I have this @client.expects(:create_config).once.returns(true) then am I right in assuming that the code in create_config() won't be run and instead true will just be returned? ...

Some commands hang my Ruby web app

I'm playing around with Rails & Sinatra, and I want to execute commands on the server. Those commands are entered from a form. The thing is, if I enter a command which expects input, my whole app hangs. Here's the code I'm using to execute them: @threads << Thread.new do Thread.current["buffer"] = "" puts "starting #{params[:com...

What is the cleanest way to do complicated branch testing?

We have a workflow need to be tested. So there are so many branches (pathes) need to test Now we using shoulda contextual test like this: context "workflow one" do setup do #do something end context "branch 1 succuess" do should "something" do end context "branch 2 succuess" do should "something" do ...

Two questions related to Ruby threads

First one: how can I create a Thread that doesn't start right away.If I use initialize without a block an exception gets raised. how can I subclass Thread, so that I may add some custom attributes, but keep the same functionality as the base Thread class? I'd also like to not have to use the initialize(&block) method for this. To bet...

map for a single value

Hi, There are a string and a proc object. The proc object is used to manipulate the string. s = "10" p = Proc.new { |i| i.to_i } [s].map(&p).first #=> 10 This works. But is there a better way to do that when s and p are given? Thanks. Sam ...

C# equivalent of the Ruby symbol.

Hi, I'm developing a little C# application for the fun. I love this language but something disturb me ... Is there any way to do a #define (C mode) or a symbol (ruby mode). The ruby symbol is quite useful. It's just some name preceded by a ":" (example ":guy") every symbol is unique and can be use any where in the code. In my case i'...

In Ruby, how do I check if method "foo=()" is defined?

In Ruby, I can define a method foo=(bar): irb(main):001:0> def foo=(bar) irb(main):002:1> p "foo=#{bar}" irb(main):003:1> end => nil Now I'd like to check if it has been defined, irb(main):004:0> defined?(foo=) SyntaxError: compile error (irb):4: syntax error, unexpected ')' from (irb):4 from :0 What is the proper syntax to use...

Ruby - Setting property values when initialing object

Lets say I have the following class: class Test attr_accessor :name end When I create the object, I want to do the following: t = Test.new {Name = 'Some Test Object'} At the moment it results in name being nil still. Is this possible? Note: I don't want to add an initialiser Thanks Ben ...

ignoring errors and proceeding in ruby

whenever there is an exception call that is raised, the script terminates. do i have to resort to putting each action ? it gets very complicated fast..... begin #someaction begin #someaction2 rescue end rescue end ...

LDAP and Ruby on Rails

I'm trying to connect to an existing LDAP server to authenticate users trying to use my app – I'm having trouble figuring out where to start. I've read about LDAP, understand how it works, and have looks at libs like ActiveLdap. I'm just not sure where to start from here: I have the hostname of the server I was to authenticate agains...

Can I initialize a class variable using a global variable? (ruby)

Do I have create extra method for this kind of assignment? @@variable = @global_variable Why? I want to have some variables that hold values and definitions to be accessible all through my script and have only one place of definition. @global_variable = 'test' class Test @@variable = @global_variable def self.display puts @@va...

Ruby on Rails ActiveRecord: table with foreign keys in more than one other table

I'm new to Ruby on Rails and I'm trying to model my table relationships. To simplify the problem, let's say I have 3 tables: -- Customer (id, address_id, ...) -- Employee (id, address_id, ...) -- Address (id, ...) Would the Address model have the following? has_one :customer has_one :employee I know this is true in the case of a sin...

ruby string encoding

So, I'm trying to do some screen scraping off of a certain site using nokogiri, but the site owners failed to specify the proper encoding of the page in a <meta> tag. The upshot of this is that I'm trying to deal with strings that think they're utf-8, but really aren't. (If you care, here are the files I was using to test this: main ...

Continue after exception in RSpec

Is there any way to have RSpec continue processing specifications after an exception is raised? This is what my spec task looks like: SPEC_PATTERN = "spec/**/*_spec.rb" Spec::Rake::SpecTask.new() do |t| t.spec_files = FileList[SPEC_PATTERN] t.verbose = true t.spec_opts = ["--format", "html:spec/spec_report.html"] t.fail_on_erro...