Some comments on new 1.9 features

Discussion in 'Ruby' started by Trans, Nov 4, 2005.

  1. Trans

    Trans Guest

    Just looking at http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9.
    Nicely done!

    Comment on a couple things

    1) ;; instead of end

    It is possible to use ;; instead of end.

    (1..100).inject do |s,x|
    s+x
    ;; # => 5050

    class Foo
    def foo; "foo" end
    ;;
    Foo.new.foo # => "foo"

    What's the rationle here? I'd rather have a punctutation mark for 'do'
    (yes, ':' again ;-p)

    (1..100).inject: |s,x|
    s+x
    end # => 5050

    Of course could have both:

    (1..100).inject: |s,x|
    s+x
    ;; # => 5050


    2) Block local variables

    Used as follows:

    # {normal args; local variables}
    d = 2
    a = lambda{|;d| d = 1}
    a.call()
    d # => 2

    When a variable is shadowed, ruby1.9 issues a warning:

    -:2: warning: shadowing outer local variable - d

    Come on. This stinkos. At some point I think you have to give it up and
    allow a declaration.


    3) Calling Procs without #call/#[]

    You can now do:

    a = lambda{|*b| b}
    (a)(1,2) # => [1, 2]

    Note that you need the parentheses:

    a = lambda{|*b| b}
    a(1,2) # => ERROR: (eval):2: compile error...

    I know this has been deprecated, but what causes this not to work
    exactly?


    4) send doesn't call private methods anymore

    ruby-talk:153672 It is still possible to call them with the newly
    introduced #funcall method.

    class Foo; private; def foo; end; end
    Foo.new.funcall:)foo) # => nil
    Foo.new.send:)foo) # => ERROR: private method `foo' called for
    #<Foo:0xb7e0e540>

    No #funcall please, I already have enough methods to make exceptions
    for in BlankSlate/BasicObject. Use #instance_send instead. Thank you.


    5) Class of singleton classes

    singleton class inherits Class rather than its object's class

    class X;end; x=X.new; class << x; self < X; end # => true

    vs. (1.8)

    class X;end; x=X.new; class << x; self < X; end # => nil

    Is this example backwards? I'm confused. Please confirm.


    6) Class variables are not inherited

    ruby-dev:23808

    class A; @@a = 1; end; class B < A; @@a end # => ERROR: (eval):1:
    uninitialized ...

    vs.

    class A; @@a = 1; end; class B < A; @@a end # => 1

    Love it! Thank you!


    T.
     
    Trans, Nov 4, 2005
    #1
    1. Advertisements

  2. Hi,

    At Fri, 4 Nov 2005 12:27:09 +0900,
    Trans wrote in [ruby-talk:164082]:
    Why do you all want colon to overwork so much? ;)
     
    nobuyoshi nakada, Nov 4, 2005
    #2
    1. Advertisements

  3. Hi --

    Not all of us -- I'm still in the "conservative about new punctuation"
    camp :)


    David
     
    David A. Black, Nov 4, 2005
    #3
  4. Please don't add ;; thats

    1) Sheer uglyness
    2) Parses as a noop operation.

    Brian
     
    Brian Schröder, Nov 4, 2005
    #4
  5. Trans

    Dave Burt Guest

    Hi.
    Aren't you the "conservative about most things" camp, David?

    Do we all not like ";;"?

    Cheers,
    Dave
     
    Dave Burt, Nov 4, 2005
    #5
  6. Hi --

    In Ruby development, or in life? :)
    As a pointless no-op, it's great :) Is it really being considered as
    a synonym for 'end'? I don't understand that at all.


    David
     
    David A. Black, Nov 4, 2005
    #6
  7. 3) Going to confuse all the people coming from Lisp...
    4) Really ugly


    Keith
     
    Keith Fahlgren, Nov 4, 2005
    #7
  8. Trans

    ts Guest

    K> 3) Going to confuse all the people coming from Lisp...

    perhaps the persons coming from Caml will be happy ...

    Guy Decoux
     
    ts, Nov 4, 2005
    #8
  9. --8323328-1617525239-1131115704=:27223
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1617525239-1131115704=:27223"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1617525239-1131115704=:27223
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    And, closer to home, all the people coming from Ruby :)


    David

    --=20
    David A. Black

    --8323328-1617525239-1131115704=:27223--
    --8323328-1617525239-1131115704=:27223--
     
    David A. Black, Nov 4, 2005
    #9
  10. Trans

    Ara.T.Howard Guest

    an people who don't like to move their fingers off of the

    a s d f j k l ;

    ;-)

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | anything that contradicts experience and logic should be abandoned.
    | -- h.h. the 14th dalai lama
    ===============================================================================
     
    Ara.T.Howard, Nov 4, 2005
    #10
  11. The funny thing about this example is that the whole point of having
    ";;" be "end" is that you could write

    class Foo
    def foo; "foo" ;;
    end

    right? Even so, I really don=E2=80=99t see the point, beyond being able =
    to
    write the following:

    if something
    if something_else
    =E2=8B=AE
    else
    =E2=8B=AE
    ;;;;

    other_statement

    Sort of semi-pythonish or something.

    nikolai

    --=20
    Nikolai Weibull: now available free of charge at http://bitwi.se/!
    Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
     
    Nikolai Weibull, Nov 4, 2005
    #11
  12. To save one character?

    class Foo
    def foo; "foo" end
    end

    ??? That parses today, right?

    James Edward Gray II
     
    James Edward Gray II, Nov 4, 2005
    #12
  13. Block local variables:

    # {normal args; local variables}
    d = 2
    a = lambda{|;d| d = 1}
    a.call()
    d # => 2

    I think embedding language specific items in comments is silly since
    you then have no mechanism to comment them out. Create a directive for
    these extensions so that they behave as other parts of the language and
    abide by the standard placement rules. Such as, things that are
    commented out are not executed and thus ignored.
     
    Dale Martenson, Nov 4, 2005
    #13
  14. Nikolai Weibull wrote:
    [CUT]
    Why don't use just : for single statement methods?

    class Foo
    def foo: "foo"
    end

    or in this case

    class Foo:
    def foo: "foo"

    It sounds more expressive than do .. ;;
     
    Domenico De Felice, Nov 4, 2005
    #14
  15. Trans

    Trans Guest

    The comment was simply that, to indicate what the semi-colon is doing.
    It has no function. Nonetheless to me its a good indication of a better
    way:

    d = 2
    a = lambda{|d| local d = 1}
    a.call()
    d # => 2

    Self commenting.

    T.
     
    Trans, Nov 4, 2005
    #15
  16. Trans

    Trans Guest

    Sorry, that should be

    d = 2
    a = lambda{|d| d = 1}
    a.call()
    d # => 2

    Otherwise:

    d = 2
    a = lambda{|d| share d = 1}
    a.call()
    d # => 1

    T.
     
    Trans, Nov 4, 2005
    #16
  17. Hey! I didn=E2=80=99t say that it was great. I just showed what I thoug=
    ht was
    the rationale behind having ;; in Ruby. Don't blame me for its
    existance. Anyway, it=E2=80=99s not about saving one character. It=E2=80=
    =99s about
    having the ; have a better counterpart than end in this particular
    instance. I didn=E2=80=99t say that it=E2=80=99s worth having just for t=
    his, though.

    nikolai

    --=20
    Nikolai Weibull: now available free of charge at http://bitwi.se/!
    Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
     
    Nikolai Weibull, Nov 4, 2005
    #17
  18. I didn't mean to blame anyone. I was just trying to understand. :)
    For a fitting counterpart, I like

    class Foo
    def foo() "foo" end
    end

    James Edward Gray II=
     
    James Edward Gray II, Nov 4, 2005
    #18
  19. Trans wrote:
    [CUT]
    IMO it would be even better to use a special character in front of the
    name, such as for @instance and @@class attributes, to distinguish
    among normal and local scope. However I can't think which character
    would look less weird :)
    maybe &d to make d local-scoped.
     
    Domenico De Felice, Nov 4, 2005
    #19
  20. Sorry for the misunderstanding.

    I like the "local" and "share" directives. That would make it clear.

    Using too many special characters clutter the code and worsen
    readability.
     
    Dale Martenson, Nov 4, 2005
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.