Curious...

11 views
Skip to first unread message

Dwyer, Mike

unread,
May 5, 2013, 1:42:58 AM5/5/13
to phillyrb
Ok, I was playing around with an idea and used an idiom from c# and other OOP languages and sort of naturally did this:

class Grid
attr_accessor :cells

def cell(x,y)
puts "return via x,y coordinate"
end

def cell(pos)
puts "return via ordinal position"
end
end

g = Grid.new

c1 = g.cell(0) # works fine

c2 = g.cell(0,0) # blows up


I know why c2 fails. I just wanted to get a cell by coordinate or ordinal position. In other OOP languages, the methods are unique by signature (method name and parameters). Doesn't look like I can use that in Ruby.

What do you do in a similar situation or am I just "not thinking in Ruby" when I attemped that?






--
Thanks,

Mike

Justin Campbell

unread,
May 5, 2013, 6:21:08 AM5/5/13
to phil...@googlegroups.com
Ruby can't have multiple methods with different arity or type signatures. Your 2nd def is reopening/clobbering the first one.

You could name them differently, or you could use *args and make a decision about how to query (but still probably delegating to named methods like cell_by_pos and cell_by_coord).

You could also use a Hash or Ruby 2's named parameters: def cell(x: nil, y: nil, pos: nil).

The thing sticking out for me is the 'x, y'. Those are not really independent values, but instead a coordinate. You could create a class for that:

Coordinate = Struct.new(:x, :y)

Hope my 6am just woke up rambling helped.

-Justin

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phillyrb+u...@googlegroups.com.
To post to this group, send email to phil...@googlegroups.com.
Visit this group at http://groups.google.com/group/phillyrb?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Maurício Linhares

unread,
May 5, 2013, 2:24:25 AM5/5/13
to phil...@googlegroups.com
Ruby doesn't have method overloading, so you basically can't do that
and unless you have a really nice reason to, you should not try to do
it at all. Just create another method with a different name.

If you think you have a really awesome reason to do that, you could
follow the (ugly) implementation for the "[]" method in Array (
https://github.com/ruby/ruby/blob/017f0ffe1822264a8ead114f0b6055805185c0ac/array.c#L1164
).

Your implementation would then look like:

class Grid
attr_accessor :cells

def cell(*args)
case args.size
when 1
puts "return via ordinal position"
when 2
puts "return via x,y coordinate"
end
end

end

The * is equivalent to the "params" keyword in C#. Not ideal, but
that's as close as you will probably get to method overloading.
-
Maurício Linhares
http://mauricio.github.io/ - http://twitter.com/#!/mauriciojr

Mat Schaffer

unread,
May 5, 2013, 11:58:58 AM5/5/13
to phil...@googlegroups.com
I also found this little nugget: http://rubydoc.info/github/rubyworks/platypus/master/file/SPEC.rdoc#Overloadable

Not to say you "should" do it this way, but it's a nice reminder of how flexible ruby can be. 


--

-Mat


Dwyer, Mike

unread,
May 5, 2013, 12:10:47 PM5/5/13
to phillyrb
Thanks Guys... I knew my method was being clobbered once I ran the code. It was just an old habit from c# - one of those "doh!" moments :)

I was just curious about if/how/when this would be useful in ruby. After seeing the example code, I'd rather go with the ruby idiom of using a different name.


Thanks,

Mike
Reply all
Reply to author
Forward
0 new messages