(url_for) and (form action in tests)

(Moving what used to be a page to a post)

Today was the test day. I was happily coding away tests until i ran into this issue. I had a form and i wanted to check for the form action. I did an assert_select on the form based on the action attribute and and i was using url_for for equality. In other code, i did something like:

assert_select “form[class=my_form_class_name][action=?]“, url_for(:controller => :my_form_controller, :action => :my_form_action)

And this was what was causing the problem. In my test scenario, url_for was generating absolute urls and in the development/production scenario, it was giving root relative urls. So, the assert_select was always failing and it took me a while to understand what was actually happening.

Parallel assignment in Ruby – gotcha

1. x, y = 1, 2 results in x = 1 and y = 2
2. x = [1, 2, 3] results in x = [1, 2, 3]
3. x, = [1, 2, 3] results in x = 1

So, what would
x, z = [1, 2, 3] result in? x = 1 and y = [2, 3]? I thought so, but no it doesn’t. It results in x = 1 and z =2.

Update: This is where the splat operator (*) comes into picture.

Blogged with the Flock Browser

Tags: , ,

alias in Ruby

Yipeee, i have stared using alias in Ruby. One typo that i often run into it doing “alias new_name, old_name” viz. putting the comma between the old and the new names. Gotta be more careful about it.

Background: I wanted to override a method in the Autotest class of the Zentest plugin to make it run the tests for 2 or 3 different conditions (enforced by the setting of a global variable). So what i did was to redefine the run_tests method in Autotest class to make the core loop (open(” | #{test_cmd}…”)) run inside a loop with a new env setting everytime.

Ruby is AWESOME!!

Blogged with the Flock Browser

Tags: ,

TRPL error

In the section 3.8.2 (Object lifetime) of The Ruby Programming Language book, it says:

The built-in Ruby classes described in this chapter have literal syntaxes, and instances of these classes are
created simply by including their values literally in your code. Objects of other classes need to be explicitly
created, and this is most often done with a method named new:
myObject = myClass.new

This is wrong cos there can never be a class of name “myClass” cos “class/module name must be CONSTANT“.  

Blogged with the Flock Browser

Tags: ,

stupidity in including gems

there was a mistake and hence a lesson. i had installed the ec2 gem and tried using it. i put the example file in directory and called it ec2. when i tried executing the file, it failed saying it cannot load ‘amazon/ec2′. after monkeying around for a while, i realized my folly of having named the test file ec2 and requiring a ruby gem of the same name from lib. i renamed my test script and everything was fine since then. :-)   

Blogged with the Flock Browser

Tags:

callers of me in ruby

want to know who called the current method? check the caller array. caller[0] is the caller, caller[1] is the caller of the caller and so on.

Blogged with the Flock Browser

Tags:

Capsistrano internals

There are lots of material out there on Capistrano, but not all of them will be as good as the cap sources themselves. I wanted to know how the cap thingy works internally and hence took a look at the cap rubygem sources. Here’s what happens on a deploy: deploy is a cap task which updates the sources from the svn and restarts the server. Now based on what kind of servers you’re using, you can write your own restart task. Cap 1.4.1 by default goes with FastCGI, you can do apachectl if you use apache etc. And if you use mongrels, you can use the mongrel_cluster gem to do stuff for you.

Besides, i have a lot of more discoveries that i should be documenting about. Just that im not getting much time.

Blogged with the Flock Browser

Tags: , ,

Deleting data from database and executing arbitrary queries

Use the delete_all method in ActiveRecord::Base to delete rows by a given criterion. Item.delete_all(criterion) will generate a query of sort DELETE FROM items where (criterion)

The delete method works diffently though. Item.delete(condition) will generate the query DELETE FROM items where id in (condition)

So, thats about delete. If i want to generate a query string in database, i’ll do:
ActiveRecord::Base.connection.execute(my_query_string)

Blogged with the Flock Browser

Tags:

Method references in Ruby – function pointers in C

I wanted to take a method reference and invoke it based on certain conditions. I googled for it and i ran into this page: http://objectmix.com/ruby/332649-passing-method-references-python-ruby.html

They list numerous methods here and i’m just simply astounded

1. using send – i used this
send(method_name, args…)

2. lambda – i have yet to understand lambdas and Procs fully
algorithms = {
:first_way => lambda {|a,b| ‘Tastes great.’},
:second_way => lambda {|a,b| ‘Less filling’},
# more algorithms here…
}
# …
puts algorithms[:first_way][nil, nil]
puts algorithms[:second_way][nil, nil]

3.taking method references with method()
def firstWay(arg1, arg2)
  return ‘Tastes great.’
end

 def secondWay(arg1, arg2)
   return ‘Less filling.’
end

def doStuff(whichway, first_arg, second_arg)
   return whichway.call(first_arg, second_arg)
end

puts doStuff(method(:firstWay), nil, nil

And also, a few methods based on ‘defining classes, and methods’ in runtime, based on procs etc.

This is just too awesome.

Blogged with the Flock Browser

Tags:

dynamic finders and arrays

Dynamic find_by methods don’t work with arrays. For more info: http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work

Blogged with the Flock Browser

Tags: ,

Follow

Get every new post delivered to your Inbox.