# Passing a method as a paramter

**URL:** <https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772>\
**Category:** Ruby\
**Created:** [March 2, 2009, 10:50am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772 "2009-03-02T10:50:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 10:50am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/1 "2009-03-02T10:50:49Z")

</div>

I’m probably missing some basic Ruby syntax, but I’m relatively new to  
Ruby and can’t figure this out from the various references I’ve looked  
at

I’d like to use this nifty test helper:

# Calls creation\_method with nil values for field\_names and asserts

that

# the resulting object was not saved and that errors were added for

that field.

# 

# assert\_required\_fields :create\_article, :subject, :body, :author

def assert\_required\_fields(creation\_method, \*field\_names)  
field\_names.each do |field|  
record = send(creation\_method, field =\> nil)  
assert\_equal false, record.valid?  
assert\_not\_nil record.errors.on(field)  
end  
end

However, I don’t know how to pass the creation method. My basic test  
would be:

def test\_should\_create\_user  
user = User.create(:email =\> “removed\_email\_address@domain.invalid”, :lastname =\> “jones”,  
:password =\> “secret”);  
assert user.valid?, “User was invalid:\n#{user.to\_yaml}”  
end

I tried this for the failure case…  
def test\_should\_not\_create\_user\_unless\_default\_fields  
assert\_required\_fields User::create, :email, :lastname, :password  
end

But that reports an error:

1. Error:  
test\_should\_not\_create\_user\_unless\_default\_fields(UserTest):  
TypeError: #\<User uuid: nil, firstname: nil, lastname: nil, login: nil,  
email: nil, …\> is not a symbol

Can someone advise me of the correct syntax?

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 11:27am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/2 "2009-03-02T11:27:30Z")

</div>

> ```
> assert_required_fields User::create, :email, :lastname, :password
> 
> ```

Instead of User::create, which doesn’t return the method as it would  
in some other languages but calls it right away, you could use either  
one of:

User.method(:create)  
lambda {|\*args| User.create(\*args)}

HTH

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 11:32am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/3 "2009-03-02T11:32:54Z")

</div>

Hi Leo,

This gave me the same error:  
assert\_required\_fields User.method(:create), :email, :lastname,  
:password

I got a different error with this:  
assert\_required\_fields {|\*args| User.create(\*args)}, :email, :lastname,  
:password

test/unit/user\_test.rb:15: syntax error, unexpected ‘,’, expecting kEND  
assert\_required\_fields {|\*args| User.create(\*args)}, :email,  
:lastname, :password

Am I missing something?

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 11:45am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/4 "2009-03-02T11:45:24Z")

</div>

Wow. Awesome run through of various Ruby syntax options – I think I’ll  
print that out and hang it on my wall. Oddly none of those match the  
original definition which uses send but has just a single  
“creation\_method” parameter…

> > Â # Â assert\_required\_fields :create\_article, :subject, :body, :author  
> > Â def assert\_required\_fields(creation\_method, \*field\_names)  
> > Â Â field\_names.each do |field|  
> > Â Â Â record = send(creation\_method, field =\> nil)  
> > Â Â Â assert\_equal false, record.valid?  
> > Â Â Â assert\_not\_nil record.errors.on(field)  
> > Â Â end  
> > Â end

Any thoughts on how that could be called?

Thanks,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 11:56am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/5 "2009-03-02T11:56:57Z")

</div>

On Mon, Mar 2, 2009 at 7:44 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > Â Â Â assert\_not\_nil record.errors.on(field)  
> > > Â Â end  
> > > Â end
> 
> Any thoughts on how that could be called?

It’s the second example, you cannot obtain an instance of Method like  
you would do in Python or JavaScript by simply omitting the  
parenthesis, you have to use the Object#method method.

All of these are equivalent:

User.create  
User.create()  
User.method(:create).call  
User.send(:create)

^ manveru

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 11:36am UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/6 "2009-03-02T11:36:55Z")

</div>

On Mon, Mar 2, 2009 at 6:49 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â #  
> Â # Â assert\_required\_fields :create\_article, :subject, :body, :author  
> Â def assert\_required\_fields(creation\_method, \*field\_names)  
> Â Â field\_names.each do |field|  
> Â Â Â record = send(creation\_method, field =\> nil)  
> Â Â Â assert\_equal false, record.valid?  
> Â Â Â assert\_not\_nil record.errors.on(field)  
> Â Â end  
> Â end

## Using blocks:

def assert\_required\_fields(\*field\_names, &block)  
field\_names.each do |field\_name|  
record = yield(field\_name =\> nil)  
assert\_equal(false, record.valid?)  
assert\_not\_nil(record.errors.on(field)  
end  
end

# Usage:

assert\_required\_fields(:email, :lastname, :password){|field|  
User::create(field) }

## Using the method itself

def assert\_required\_fields(method, \*field\_names)  
field\_names.each do |field\_name|  
record = method.call(field\_name =\> nil)  
assert\_equal(false, record.valid?)  
assert\_not\_nil(record.errors.on(field)  
end  
end

# Usage

assert\_required\_fields(User.method(:create), :email, :lastname,  
:password)

## Using send

def assert\_required\_fields(obj, method, \*field\_names)  
field\_names.each do |field\_name|  
record = obj.send(method, field\_name =\> nil)  
assert\_equal(false, record.valid?)  
assert\_not\_nil(record.errors.on(field)  
end  
end

# Usage

assert\_required\_fields(User, :create, :email, :lastname, :password)

^ manveru

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/7 "2009-03-02T17:30:27Z")

</div>

On Tue, Mar 3, 2009 at 12:16 AM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > you would do in Python or JavaScript by simply omitting the
> 
> I get the error: (ActiveRecord::Base).create\> is not a symbol  
> calling:  
> Â assert\_required\_fields User.method(:create), :create, :email,  
> :lastname, :password

Sorry, with rails all bets on normally behaving ruby code are off.

^ manveru

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/8 "2009-03-02T16:17:46Z")

</div>

Michael F. wrote:

> On Mon, Mar 2, 2009 at 7:44 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > Â Â Â assert\_not\_nil record.errors.on(field)  
> > > > Â Â end  
> > > > Â end
> > 
> > Any thoughts on how that could be called?
> 
> It’s the second example, you cannot obtain an instance of Method like  
> you would do in Python or JavaScript by simply omitting the  
> parenthesis, you have to use the Object#method method.
> 
> All of these are equivalent:
> 
> User.create  
> User.create()  
> User.method(:create).call  
> User.send(:create)
> 
> ^ manveru

I get the error: (ActiveRecord::Base).create\> is not a symbol  
calling:  
assert\_required\_fields User.method(:create), :create, :email,  
:lastname, :password

the code in my library uses send, not call

Of course, I could just change the definition of assert\_required\_fields,  
but since it is part of a plugin  
([http://topfunky.net/svn/plugins/topfunky\_power\_tools/lib/topfunky/test\_helper.rb](http://topfunky.net/svn/plugins/topfunky_power_tools/lib/topfunky/test_helper.rb)  
), I figure there must be some standard way to call it

Thanks for your help,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/9 "2009-03-02T19:07:13Z")

</div>

On Mon, Mar 2, 2009 at 10:16 AM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > you would do in Python or JavaScript by simply omitting the
> 
> [http://topfunky.net/svn/plugins/topfunky\_power\_tools/lib/topfunky/test\_helper.rb](http://topfunky.net/svn/plugins/topfunky_power_tools/lib/topfunky/test_helper.rb)  
> ), I figure there must be some standard way to call it

Yes, you need to pass a symbol, just as the error is trying to tell you:

def test\_should\_not\_create\_user\_unless\_default\_fields  
assert\_required\_fields :create, :email, :lastname, :password  
end

I’m assuming that this is in the unit test for the User model.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/10 "2009-03-02T19:42:53Z")

</div>

Rick Denatale wrote:

> Yes, you need to pass a symbol, just as the error is trying to tell you:
> 
> def test\_should\_not\_create\_user\_unless\_default\_fields  
> assert\_required\_fields :create, :email, :lastname, :password  
> end
> 
> I’m assuming that this is in the unit test for the User model.

So, how would it magically know to call create on User and not some  
other class? as I might expect, I get this error:

1. Error:  
test\_should\_not\_create\_user\_unless\_default\_fields(UserTest):  
NoMethodError: undefined method `create’ for #UserTest:0x1e93a58

Thanks,  
Sarah:w

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 9:18pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/11 "2009-03-02T21:18:56Z")

</div>

On Mon, Mar 2, 2009 at 1:41 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> other class? as I might expect, I get this error:
> 
> 1. Error:  
> test\_should\_not\_create\_user\_unless\_default\_fields(UserTest):  
> NoMethodError: undefined method `create’ for #UserTest:0x1e93a58

Okay, I’m curious where you found the topfunky power tools plugin.  
There’s  
not much that google reveals about the usage.

However, it appears to come from a very early Peepcode screen cast on  
Test  
First Design (Peepcode #4 to be exact). I did a quick look at the  
screencast and it depends on having helper method called ceate in the  
test  
case. So you should have something like:

class UserTest \< Test::Unit::TestCase  
#…  
def test\_should\_not\_create\_user\_unless\_default\_fields  
assert\_required\_fields :create, :email, :lastname, :password  
end

private  
def create(options={})  
User.create({  
:email =\> “[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”,  
:last\_name =\> “jones”,  
:password =\> “secret”  
}.merge(options)  
)  
end  
end

The create method creates a valid record, and the assert\_required\_fields  
nils out each field asserted to be required, calls the create method  
with  
that field set to nil and ensures that the resulting record has an error  
on  
that field.

And given that helper method, the other test can be re-written as:

def test\_should\_create\_user  
user = create  
assert user.valid?, “User was invalid:\n#{user.to\_yaml}”  
end

Now, as I say, this plugin doesn’t seem to be very popular based on a  
google  
search, and it’s rather old. There are probably more ‘modern’  
approaches to  
testing required field validations these days using Test::Unit tests in  
Rails, I’m not as up on that as I tend to use RSpec rather than  
Test::Unit.

## And since this really IS a Rails question, it probably would have gotten a better response, quicker if it had been submitted ot the ruby on rails mailing list.

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/12 "2009-03-02T22:20:25Z")

</div>

Rick Denatale wrote:

> Okay, I’m curious where you found the topfunky power tools plugin.  
> There’s not much that google reveals about the usage.

I’m just getting into TDD. I experimented with cucumber a bit and really  
liked it, but I’m diving into an old codebase with lots of pre-existing  
unit tests. I wanted to understand the basics, so (as you guessed) I  
bought the peepcode tutorial.

> Now, as I say, this plugin doesn’t seem to be very popular based on a  
> google search, and it’s rather old. There are probably more ‘modern’  
> approaches to  
> testing required field validations these days using Test::Unit tests in  
> Rails, I’m not as up on that as I tend to use RSpec rather than  
> Test::Unit.

If its wisdom is out of date, is there another resource you recommend?  
Or is everyone just skipping unit tests these days in favor of the BDD  
tools?

> And since this really IS a Rails question, it probably would have gotten  
> a better response, quicker if it had been submitted ot the ruby on rails  
> mailing list.

Yeah, I see that in retrospect – seemed like a basic language question  
at the time. Thanks so much for digging in and letting me know the  
scoop, which I clearly didn’t fully understand from the screencast. Of  
course, now I know four ways to pass a method as a parameter, which is  
fabulous.

Cheers,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/passing-a-method-as-a-paramter/161772/13 "2009-03-02T21:23:40Z")

</div>

On Mar 2, 2009, at 3:16 PM, Rick DeNatale wrote:

> Okay, I’m curious where you found the topfunky power tools plugin.  
> There’s  
> not much that google reveals about the usage.
> 
> However, it appears to come from a very early Peepcode screen cast  
> on Test  
> First Design (Peepcode #4 to be exact). I did a quick look at the  
> screencast and it depends on having helper method called ceate in  
> the test  
> case. So you should have something like:

Wow. +10 to Rick for his google-ruby-rails-test-unit-mentoring-fu

Gary W.
