Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

undefined would be rare

    foo
    //Uncaught ReferenceError: foo is not defined
if not property access, and it lies

    o = { foo: undefined }
    o.foo 
    //undefined
    o.hasOwnProperty('foo')
    //true
Array is an object, access by index is property access

    a = []
    a[0]
    //undefined
That's unfortunate. In Ruby:

    foo
    #NameError (undefined local variable or method `foo' for main:Object)
    defined?(foo)
    #=> nil

    o = Object.new
    o.foo
    #NoMethodError (undefined method `foo' for #<Object:0x000055f2f4e56e78>)

    a = []
    a[0]
    #=> nil
And Ruby enforces method arity and keyword parameters:

    def foo(value)
    end
    foo
    #ArgumentError (wrong number of arguments (given 0, expected 1))
    
    def bar(value:)
    end
    bar
    #ArgumentError (missing keyword: :value)
Basically it throws on undefined.

I think reimplementation is a great way to uncover how things work, Ruby "fix" for property access

http://sergeykish.com/ruby-like-javascript-undefined.rb

Anyone knows how to relax arity?



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: