# Shorthand bean constructor?

**URL:** <https://www.ruby-forum.com/t/shorthand-bean-constructor/205191>\
**Category:** JRuby\
**Created:** [March 31, 2011, 3:04pm UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191 "2011-03-31T15:04:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 31, 2011, 3:04pm UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191/1 "2011-03-31T15:04:57Z")

</div>

Let’s say I have a Book class in Java that has an author and title  
fields.  
I think in Groovy I could write something like:

def book = new Book(title: “Foo”, author: “Bar”)

and Groovy would automatically construct the bean and call the setters  
for  
those fields. It’s just a shorthand way of constructing Java beans I  
think.  
Is there anything like this with the Java integration that JRuby has?  
I  
was hoping for something like:

book = Book.new({:title =\> “Foo”, :author =\> “Bar”)

but that doesn’t work. Anyone know of something like this?

Joe

---

<div class="post-metadata">

**Author:** ![Joseph\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Joseph\_A](https://www.ruby-forum.com/u/Joseph_A)\
**Post date:** [March 31, 2011, 5:06pm UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191/2 "2011-03-31T17:06:50Z")

</div>

There’s nothing built-in, but you can easily write your own.

module BeanLikeConstructor  
def new(attrs = {})  
super().tap |obj|  
attrs.each do |k,v|  
obj.send("#{k}=", v) if obj.respond\_to?("#{k}=")  
end  
end  
end  
end

class Book  
extend BeanLikeConstructor  
end

book = Book.new(:title =\> “Foo”, :author =\> “Bar”)

/Nick

---

<div class="post-metadata">

**Author:** ![Joseph\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Joseph\_A](https://www.ruby-forum.com/u/Joseph_A)\
**Post date:** [April 1, 2011, 3:34am UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191/3 "2011-04-01T03:34:27Z")

</div>

On Thu, Mar 31, 2011 at 11:05 AM, Nick S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> class Book  
> extend BeanLikeConstructor  
> end
> 
> book = Book.new(:title =\> “Foo”, :author =\> “Bar”)

I’m pretty sure many other ways would be there. I think of below:

proc = lambda {|k,v| b = Book.new; b.set\_title(k); b.set\_author(v); b}  
book = proc.call(‘Foo’, ‘Bar’)

-Yoko

---

<div class="post-metadata">

**Author:** ![Joseph\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Joseph\_A](https://www.ruby-forum.com/u/Joseph_A)\
**Post date:** [April 4, 2011, 5:48am UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191/4 "2011-04-04T05:48:00Z")

</div>

How cool, Nick.  
Don’t mean to derail the thread, but I didn’t know about “tap”. Very  
sweet  
method!

Cheers,  
–Bob

---

<div class="post-metadata">

**Author:** ![Joseph\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Joseph\_A](https://www.ruby-forum.com/u/Joseph_A)\
**Post date:** [March 31, 2011, 11:13pm UTC](https://www.ruby-forum.com/t/shorthand-bean-constructor/205191/5 "2011-03-31T23:13:18Z")

</div>

Thanks, that’s what I figured. I’m giving a little presentation on  
JRuby  
and I wanted to show all the handy features you can utilize. Would  
there be  
any desire to add this to the Java integration? Seems like a handy  
thing to  
have.

Joe
