January 2018

S M T W T F S
  123456
78910111213
14151617181920
21222324252627
28293031   

Style Credit

Expand Cut Tags

No cut tags
Tuesday, September 11th, 2007 07:47 pm
irb(main):001:0> def fred(x); x + 1; end
nil
irb(main):002:0> fred.methods
ArgumentError: wrong # of arguments(0 for 1)
        from (irb):2:in `fred'
        from (irb):2
irb(main):003:0> 1.+.methods
ArgumentError: wrong # of arguments(0 for 1)
        from (irb):3:in `+'
        from (irb):3
irb(main):004:0> {|x| x+1}.methods
SyntaxError: compile error
(irb):4: parse error
{|x| x+1}.methods
  ^
(irb):4: parse error
{|x| x+1}.methods
         ^
        from (irb):4
irb(main):005:0> lambda {|x| x+1}.methods
["call", "==", "[]", "arity", "to_s", "dup", "eql?", "protected_methods",
 "frozen?", "===", "respond_to?", "class", "kind_of?", "__send__", "nil?",
 "instance_eval", "public_methods", "untaint", "__id__", "display",
 "inspect", "taint", "hash", "=~", "private_methods", "to_a", "is_a?",
 "clone", "equal?", "singleton_methods", "freeze", "type", "instance_of?",
 "send", "methods", "method", "tainted?", "instance_variables", "id",
 "extend"]
In unrelated news, Happy birthday [livejournal.com profile] stronae!

And does anyone if there's a portable way of finding the arity of a function in Scheme?
Tuesday, September 11th, 2007 08:20 pm (UTC)
I think that's just a syntax issue; Ruby specifies that "a.b.c" is equivalent to either "a.b().c()", or "self.a().b().c()" depending on if a is a declared local variable or not.

Try this instead:
irb> def fred(x); x+1; end
irb> method(:fred)

Tuesday, September 11th, 2007 09:20 pm (UTC)
Hurrah! So (for instance) I can find the arity of a Ruby function by calling method(:fred).arity. And it does make sense for a.b.c to be a.b().c(), because method chaining is more common than calling methods on function objects. Thanks very much!
[identity profile] david jones (from livejournal.com)
Wednesday, September 12th, 2007 01:11 pm (UTC)
It was roughly at this point, discovering that «fred» is sugar for «fred()», that in my investigation of Ruby I declared it to be madness beyond mad and vowed to never touch it again.
Wednesday, September 12th, 2007 05:10 pm (UTC)
There are times when it's nice to be able to leave the brackets out in function calls - when calling accessor methods, for instance. It's a tricky one, I think. method(:fred) is a bit heavyweight for my taste, though!
[identity profile] david jones (from livejournal.com)
Wednesday, September 12th, 2007 08:39 pm (UTC)
Heavyweight, yes, and not to mention the fact that neither you nor I found out about it without help, so that reflects poorly on the documentation.

In Common Lisp of course there's no difference between slots and methods. To get the value of slot FOO from object O you go (FOO O) and that's a method invocation, so if you want to write your own method to do that then fine. Strictly speaking you can use SLOT-VALUE to get at the slot without an accessor but I consider that bad style.
Wednesday, September 12th, 2007 10:10 pm (UTC)
That's the point, actually; to make there be no difference between member variable accessors and methods.
class Foo
  attr_accessor :x
end
is the same as
class Foo
  def x(); @x; end
  def x=(x_); @x = x_; end
end
and, in fact, that's exactly what attr_accessor does--insert that code into your class definition, and there's nothing special about how it does that, so you can write your own "code-generating" methods to do more interesting things.

In both cases foo = Foo.new; foo.x = 5; print foo.x has the same effect. And also, like lisp, you can use foo.instance_variable_set(:@x,5) and foo.instance_variable_get(:@x) to skip the encapsulation, but it's considered bad style.
Wednesday, September 12th, 2007 01:24 am (UTC)
Much thanks! It was great!

Also, I don't have an answer to your question... usually I attempt to invoke it, and the interpreter tells me how many the function was expecting. :(
Wednesday, September 12th, 2007 10:01 am (UTC)
Birthday: glad to hear it!

The reason I was asking the Scheme question should become clear once you read my next post...