News for hackers. Stuff that matters.
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by relix on Thursday June 13, 2013 @07:51AM (#5873519)
It's a common joke that Ruby is designed to make it as hard as possible for parsers.

For the code samples going forward, keep in mind that in ruby you don't need a literal return statement to return a value from a method. Just whatever value is last, is returned:

    def val
      5
    end
That method would always return 5.

> But then if the b() call itself returns a function, then is that function auto-called as well?

No, the only way to pass a method is by using method(method_name_here), which returns a Method object. To execute a Method object, you need to call call on it:

     def a
     end
     method(:a).call
So if you have a method that returns a method, it should not be automatically called, so there's no loop necessary:

     def b
       method(:a)
     end

     z = b
Then z is a Method object, namely a.

Some experiments to answer your questions (I admit I wasn't sure for all of them what the outcome would be). IRB is Ruby's repl:

    irb(main):001:0> def a
    irb(main):002:1> 5
    irb(main):003:1> end
    
    irb(main):019:0> {a: 1}
    => {:a=>1}
    irb(main):020:0> {a(): 1}
    SyntaxError: (irb):20: syntax error, unexpected ':', expecting tASSOC



    irb(main):009:0> def h(num = 10)
    irb(main):010:1> num
    irb(main):011:1> end

    irb(main):012:0> h
    => 10
    irb(main):013:0> h-3
    => 7
    irb(main):014:0> h -3
    => -3
    irb(main):015:0> h - 3
    => 7

    irb(main):001:0> a?b:c
    NameError: undefined local variable or method `c' for main:Object
Re:Confessions of a Ruby Developer Whose Heart was Stolen by... by csense on Thursday June 13, 2013 @06:30PM (#5877017)
This is really interesting. I guess some of my examples were relying on using a similar lexer as C -- which you probably couldn't in Ruby. I'll have to think some more about what a Ruby lexer would look like...

But again, it steepens the learning curve -- it's so different from other languages that it's actually kind of hard to immediately see all the implications of "well Ruby's lexer is different than C's lexer".