Live data from Hacker News

R.I.P. Ruby Hash Rocket Syntax 1993-2010

blog.peepcode.com

31–40 of 95 posts

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#31
post #5

also says this syntax is compatible with python which is not really true. if you have this in python: { key: "value", dr_nic: "The Comedian", ttl: 42 } python will expect there to be a variable called 'key', one called 'dr_nic', and one called 'ttl'. If you intend to do d['ttl'] you'll need to create a hash using a literal string

I am not a Ruby programmer. What's the syntax for using a variable as the key, which is actually synonymous with the Python code?

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#32
post #31
post #5

also says this syntax is compatible with python which is not really true. if you have this in python: { key: "value", dr_nic: "The Comedian", ttl: 42 } python will expect there to be a variable called 'key', one called 'dr_nic', and one called 'ttl'. If you intend to do d['ttl'] you'll need to create a hash using a literal string

I am not a Ruby programmer. What's the syntax for using a variable as the key, which is actually synonymous with the Python code?

[deleted]

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#37
post #11
post #5

also says this syntax is compatible with python which is not really true. if you have this in python: { key: "value", dr_nic: "The Comedian", ttl: 42 } python will expect there to be a variable called 'key', one called 'dr_nic', and one called 'ttl'. If you intend to do d['ttl'] you'll need to create a hash using a literal string

You don't necessarily have to create a hash using a literal string. You can also do this: dict( key="value", dr_nic="The Comedian", ttl=42 ) ...but that is a bit different syntax.

It will also make your code drastically slower.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#38
post #37
post #11

Earlier quoted context omitted.

You don't necessarily have to create a hash using a literal string. You can also do this: dict( key="value", dr_nic="The Comedian", ttl=42 ) ...but that is a bit different syntax.

It will also make your code drastically slower.

Slower than what alternative?

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#39
post #37
post #11

Earlier quoted context omitted.

You don't necessarily have to create a hash using a literal string. You can also do this: dict( key="value", dr_nic="The Comedian", ttl=42 ) ...but that is a bit different syntax.

It will also make your code drastically slower.

Slower yes, but not drastically:

    % python -m timeit '{"key": "value", "dr_nic": "The Comedian", "ttl": 42}'
    1000000 loops, best of 3: 0.296 usec per loop

    % python -m timeit 'dict(key="value", dr_nic="The Comedian", ttl=42)'
    1000000 loops, best of 3: 0.771 usec per loop
There are far better ways to make code faster than worrying about the extra fraction of a microsecond you're spending calling dict rather than using the literal syntax.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#40
post #31
post #5

also says this syntax is compatible with python which is not really true. if you have this in python: { key: "value", dr_nic: "The Comedian", ttl: 42 } python will expect there to be a variable called 'key', one called 'dr_nic', and one called 'ttl'. If you intend to do d['ttl'] you'll need to create a hash using a literal string

I am not a Ruby programmer. What's the syntax for using a variable as the key, which is actually synonymous with the Python code?

It would be something like this:

    >> a = :foobar
    => :foobar
    >> {a => 1}
    => {:foobar=>1}
Post reply on HN