>> class Foo
>> def initialize(a , b=nil)
>> @a = a
>> @b = b
>> end
>> end
>>
>> Foo.new(1)
>>
>
> Nah! That's not what I asked. I want to delay the instance creation of Foo, until it gets all its required arguments. For me `a` and `b` both are required arguments.
>
> Hints: Proc#curry. 
>
Well, I'd like to simple be snarky and say "don't call Foo.new until you have both prerequisite arguments", but instead, let me ask you two questions:
What would you expect Foo.new(1) to be?
Well, Look the below example first using Method#curry.
def any_method(a,b)
"called"
end
m = method(:any_method).curry
n = m.call(1) # => #<Proc:0x9872144 (lambda)>
n.call(2) # => "called"
So, with the example, we can delay the method body execution if we needed using the `#curry` method. This example leads me to think to do the same with `#new` too. But that didn't work.
class Foo
def initialize(a, b)
end
def foo_val
"object created"
end
end
Foo.method(:new).curry.call(1, 2) # => #<Foo:0x9871b54>
Foo.method(:new).curry.call(1)
# `initialize': wrong number of arguments (1 for 2) (ArgumentError)
So, even if I am currying the #new, it didn't work as the in the case of `#any_method`. If it could be done, then I might able to delay the creation of the Foo instance.
What do you envision it would look like to later supply the 'b'? (and can that value actually be nil?)
It can be any value.
-Rob
Am I clear now with my intention ?
Note: Method#curry has been added to Ruby core 2.2.0 (Class: Method (Ruby 2.2.0))
···
On Monday, February 09, 2015 02:41:58 PM Rob Biedenharn wrote:
On 2015-Feb-9, at 13:13 , Arup Rakshit <aruprakshit@rocketmail.com> wrote:
> On Monday, February 09, 2015 05:51:02 PM George Drummond wrote:
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan