Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def output name=((default=true); "caius")
puts "name: #{name.inspect}"
puts "default: #{default.inspect}"
end
output
# >> name: "caius"
# >> default: true
output "avdi"
# >> name: "avdi"
# >> default: nil
@ngauthier

This comment has been minimized.

Show comment
Hide comment
@ngauthier

ngauthier Dec 28, 2011

this is one of the weirdest ruby thing's I've ever seen. I love it.

xoxo @ngauthier

this is one of the weirdest ruby thing's I've ever seen. I love it.

xoxo @ngauthier

@caius

This comment has been minimized.

Show comment
Hide comment
@caius

caius Dec 28, 2011

I love it too, because I can understand how ruby evaluates it (I think.) The default argument is evaluated as ruby, but in the same scope as the method, so setting local variables means they're set for the method as well. And the parenthesis make it one expression, which ruby is fine with. And then it uses the return value of that expression for the default value of the name variable.

Owner

caius commented Dec 28, 2011

I love it too, because I can understand how ruby evaluates it (I think.) The default argument is evaluated as ruby, but in the same scope as the method, so setting local variables means they're set for the method as well. And the parenthesis make it one expression, which ruby is fine with. And then it uses the return value of that expression for the default value of the name variable.

@teoulas

This comment has been minimized.

Show comment
Hide comment
@teoulas

teoulas Dec 28, 2011

Wow, really interesting. Never thought about this use case, but kind of makes sense.

teoulas commented Dec 28, 2011

Wow, really interesting. Never thought about this use case, but kind of makes sense.

@avdi

This comment has been minimized.

Show comment
Hide comment
@avdi

avdi Dec 28, 2011

Holy crap, look at all the forks! You've created a monster!

avdi commented Dec 28, 2011

Holy crap, look at all the forks! You've created a monster!

@toamitkumar

This comment has been minimized.

Show comment
Hide comment
@toamitkumar

toamitkumar Dec 28, 2011

@caius @avdi - what is the use case of this trick ?

@caius @avdi - what is the use case of this trick ?

@avdi

This comment has been minimized.

Show comment
Hide comment
@avdi

avdi Dec 28, 2011

The only use case I can think of is lulz. I'd never let this through a code review.

avdi commented Dec 28, 2011

The only use case I can think of is lulz. I'd never let this through a code review.

@toamitkumar

This comment has been minimized.

Show comment
Hide comment
@toamitkumar

toamitkumar Dec 28, 2011

@avdi :) I was scratching if I would ever end up using this trick...

@avdi :) I was scratching if I would ever end up using this trick...

@ngauthier

This comment has been minimized.

Show comment
Hide comment
@ngauthier

ngauthier Dec 29, 2011

turbo lulz:

def x y=((return 'lol'); y)
  y
end

trololo @ngauthier

turbo lulz:

def x y=((return 'lol'); y)
  y
end

trololo @ngauthier

@ngauthier

This comment has been minimized.

Show comment
Hide comment
@ngauthier

ngauthier Dec 29, 2011

also

def x(y=(raise 'i <3 params')); y; end

also

def x(y=(raise 'i <3 params')); y; end
@skanev

This comment has been minimized.

Show comment
Hide comment
@skanev

skanev Dec 29, 2011

Don't forget:

def foo(something = (def foo(*); 'bar'; end)) 'qux' end

puts foo(10)  # qux
puts foo      # qux
puts foo(10)  # bar
puts foo      # bar

skanev commented Dec 29, 2011

Don't forget:

def foo(something = (def foo(*); 'bar'; end)) 'qux' end

puts foo(10)  # qux
puts foo      # qux
puts foo(10)  # bar
puts foo      # bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment