Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Can lambda function parameters have default values?

Tags:

I want to do something similar to this:

def creator()         return lambda { |arg1, arg2 = nil|                 puts arg1                 if(arg2 != nil)                         puts arg2                 end         } end  test = creator()  test('lol') test('lol', 'rofl') 

I get a few syntax errors:

test.rb:2: syntax error         return lambda { |arg1, arg2 = nil|                                  ^ test.rb:3: syntax error test.rb:7: syntax error test.rb:14: syntax error 

is this possible in ruby? i want to set a default value for a parameter to a lambda function

like image 620
asdasd Avatar asked Sep 29 '10 00:09

asdasd


People also ask

How do you pass a default parameter in Ruby?

The syntax for declaring parameters with default values In order to define a default value for a parameter, we use the equal sign (=) and specify a value to which a local variable inside the method should reference.

How does lambda work in Ruby?

In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately.

Can Python lambda have default arguments?

Defaults in Python Lambda ExpressionIn Python, and in other languages like C++, we can specify default arguments.

What is the function parameter with a default value in Scala?

Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. The parameter level has a default value so it is optional. On the last line, the argument "WARNING" overrides the default argument "INFO" .


2 Answers

In Ruby 1.9+, you can use either of the old-style lambdas or the new "arrow" lambda syntax to set a default parameter:

ruby-1.9.1-p378 > f = lambda {|x, y=1| puts(x+y) }  => #<Proc:0x000001009da388@(irb):4 (lambda)>  ruby-1.9.1-p378 > f.call(1) 2  => nil  ruby-1.9.1-p378 > f.call(1,5) 6  => nil   ruby-1.9.1-p378 > f = ->(a, b=5) { puts(a+b) }  => #<Proc:0x00000100a0e1b0@(irb):1 (lambda)>  ruby-1.9.1-p378 > f.call(1) 6  => nil  ruby-1.9.1-p378 > f.call(1,2) 3  => nil  
like image 122
Mark Rushakoff Avatar answered Dec 18 '22 19:12

Mark Rushakoff


In Ruby 1.8.x you can sort of fake it along the lines of:

def creator   lambda do |*args|     raise ArgumentError if args.empty? || args.size > 2     arg1, arg2 = args     puts arg1     puts arg2 unless arg2.nil?   end end  >> test = creator => #<Proc:0x000000010125e138@(irb):2> >> test.call("foo") foo => nil >> test.call("foo", "bar") foo bar => nil >> test.call("foo", "bar", "baz") ArgumentError: ArgumentError 

Edit: The above example defaults the second argument to nil, but if you wish to have another default you can assign arg2 based on args.size (e.g. arg2 = mydefault if args.size < 2). Similarly if you have more than two arguments the unspecified ones will default to nil unless you assign them yourself.

For Ruby 1.9+ see other answers.

like image 35
Arkku Avatar answered Dec 18 '22 18:12

Arkku