My first feeling of Ruby

Discussion in 'Ruby' started by Florent Guiliani, Oct 18, 2006.

  1. Hi all,

    Today I'm learning Ruby. Globally It's a great programming langage but
    I've noticed some irrationnal things:

    First thing:
    ============
    irb(main):138:0* class Toto
    irb(main):139:1> @@test="a"
    irb(main):140:1>
    irb(main):141:1* def test
    irb(main):142:2> @test="b"
    irb(main):143:2> test="c"
    irb(main):144:2> puts "test #@@test"
    irb(main):145:2> puts "test #@test"
    irb(main):146:2> puts "test #test" # #{test} instead #test why?
    irb(main):147:2> end
    irb(main):148:1> end
    => nil
    irb(main):149:0> Toto.new.test
    test a
    test b
    test #test
    => nil

    Why #test doesn't work for local variables? It would be more logic. It's
    my point of view. We can have both #test and #{test} working.


    Second thing:
    =============
    hastables is using a mix of {} and []. Always using {} or [] (but not
    both) for hastable would be "errorless".

    Actually {} for initialisation:
    irb(main):150:0> hash = { "A"=>1 , "B"=>2 }
    => {"A"=>1, "B"=>2}

    [] for reading:
    irb(main):152:0> hash["A"]
    => 1

    If you go wrong:
    irb(main):156:0> hash{"A"}
    => 537984250
    irb(main):154:0> hash = [ "A"=>1 , "B"=>2 ]
    => [{"A"=>1, "B"=>2}]

    I would prefer an error or warning. The last line means I can omit {}?
    so let's go on:

    irb(main):155:0> hash = "A"=>1 , "B"=>2
    SyntaxError: compile error
    (irb):155: parse error, unexpected tASSOC, expecting $
    hash = "A"=>1 , "B"=>2
    ^
    from (irb):155
    from :0
    irb(main):156:0>

    Hmm why [ "A"=>1 , "B"=>2 ] silently produce [{"A"=>1, "B"=>2}] ?

    my 2c,
    Florent,
     
    Florent Guiliani, Oct 18, 2006
    #1
    1. Advertisements

  2. Florent Guiliani

    Bira Guest

    I'm sure someone who's knows way more than me about Ruby's internals
    will supply a technical reason for it, but, personally, I prefer #{the
    current syntax}, because, to me, it's less ambiguous and allows for
    complex expressions inside the curly brackets.

    As for the hashes, it just happens that [] is the name of the method
    that retrieves a certain element from a collection. If you look at it
    this way, it becomes quite logical for both collections to have the
    same syntax.
     
    Bira, Oct 18, 2006
    #2
    1. Advertisements

  3. Florent Guiliani

    Trans Guest

    I don't most of us use those shorcuts, prefering to alwasy use #{...}
    notation:

    puts "test #{@@test}"
    puts "test #{@test}"
    puts "test #{test}"
    I have to agree with you here. But the issue is a much larger one.
    Because Ruby handles the trailing block argument as a special case it
    is always an optional argument. So any method can take a block and not
    raise an error:

    puts { blah!.nothing.matters } => "\n"

    So in your case you are actually calling the #hash method which
    actually has nothing to do with a Hash class, and your throwing in a
    meaningless block to go with it.

    I'd rather have blocks be passed in as the trailing argument
    regardless, such that the special '&argument' were not needed at all.
    This would improve method interfaces.

    On another note, I always thought it would be nice to have a merged
    Hash/Array, call it "Collection" if you will, such that

    c = Collection.new
    c[index] # lookup by index
    c{key} # lookup by key

    But that notation might cause other conflicts.
    Any method can take hash elements which are "silently" converted to a
    hash. So:

    def foo(arg)
    p arg
    end

    foo( "A"=>1 , "B"=>2 ) #=> {"A"=>1 , "B"=>2}

    The same is true for #[] which is also a method!

    Some of us howerver would like to see [ "A"=>1 , "B"=>2 ] be
    interpreted as an ordered hash instead if reasonably possible.

    HTH,
    T.
     
    Trans, Oct 18, 2006
    #3
  4. Florent Guiliani

    Trans Guest

    Trans wrote:
    Correction:
    Also, I might suggest that matz/ruby do away with those shortcuts.

    T.
     
    Trans, Oct 18, 2006
    #4
  5. And many other objects, like String and Proc. This makes for great
    duck typing.

    James Edward Gray II
     
    James Edward Gray II, Oct 18, 2006
    #5
  6. Florent Guiliani

    Tim Hunter Guest

    Welcome to Ruby!

    I recommend that you use Ruby with an open mind for 4-6 weeks before
    making any judgments about it. That gives you time to learn how things
    in Ruby fit together. At first some things may seem arbitrary but after
    a while you see that there are good reasons for Ruby to be the way it is.

    After you've finished a medium-sized project come back and let us know
    what you think.
     
    Tim Hunter, Oct 18, 2006
    #6
  7. Florent Guiliani

    Hal Fulton Guest

    For one thing, test could just as easily be a method name. This
    might lead people to think that the parameters followed it in
    the string.

    Things such as #$foo and #@foo are special cases. A single char
    tells us that it's a simple variable.

    The general case is #{...} and can be any arbitrary expression.

    Conflict with block syntax. You can't say hash {"A"} meaningfully.
    In fact, I'm not certain why that returns an integer.


    Hal


     
    Hal Fulton, Oct 19, 2006
    #7
  8. Florent Guiliani

    Hal Fulton Guest

    Absolutely not.

    Hal
     
    Hal Fulton, Oct 19, 2006
    #8
  9. Florent Guiliani

    Trans Guest

    Huh? You must know. It's the #hash method:

    irb(main):001:0> o = Object.new
    => #<Object:0xb7d06254>
    irb(main):002:0> o.hash
    => -605540054
    irb(main):003:0> o.hash { }
    => -605540054
     
    Trans, Oct 19, 2006
    #9
  10. Florent Guiliani

    Trans Guest

    Becaue you like extraneous notations that are rarely used and
    subsequently create less readiable code?

    T.
     
    Trans, Oct 19, 2006
    #10
  11. Florent Guiliani

    Phrogz Guest

    What evidence do you have to support the claim that they are rarely
    used? Also, would you concede that "less readable code" is a subjective
    criticism?

    I personally use #@foo for every (as far as I know) instance variable
    I've included as a part of my own string interpolation code.
     
    Phrogz, Oct 19, 2006
    #11
  12. Florent Guiliani

    Mait Guest

    Florent Guiliani 작성:
    Yes, you can omit {} for method argument in some condition.

    hash = [ "A"=>1 , "B"=>2 ]
    hash = Array.new << {"A"=>1 , "B"=>2}
    hash = Array.new.push("A"=>1, "B"=>2)

    Third form is regular. Think first and second case is special thing.

    quote in PickAxe book(Programming Ruby):
    I think this feature make method call interface more freely.

    method_call :eek:ne => something
    method_call :two => another
    method_call :eek:ne => something, :two => another
    method_call :two => another, :eek:ne => something, :three => this
     
    Mait, Oct 19, 2006
    #12
  13. Florent Guiliani

    x1 Guest

    I've never used it or new it existed and have now taken a note to
    watch out for it should I ever have #@something in a "string".

    Not that I know enough about ruby or development in general to speak
    on it's validity, however I would be interest in seeing the actual
    lines of code in the ruby source that checks the #@something syntax.
    Could someone share it if it's not too much trouble?

    Is the ruby logic something like this?
    if string contains "#@" and the text after the "@" (with some
    delimiter) prefixed with an "@" is a variable, interpret it??

    I cant see anyway this is what's being done because it seems horible
    lol.. What does it actually do? Please share
     
    x1, Oct 19, 2006
    #13
  14. Florent Guiliani

    Bill Kelly Guest

    Me too. Indeed, I find it more readable, personally.

    Or at least, perfectly readable, and less cluttered.


    Regards,

    Bill
     
    Bill Kelly, Oct 19, 2006
    #14
  15. And stressing the, probably, obvious:

    irb(main):001:0> hash
    => -605215560
    irb(main):002:0> self.hash
    => -605215560
     
    Rick DeNatale, Oct 19, 2006
    #15
    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.