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
R.I.P. Ruby Hash Rocket Syntax 1993-2010
31–40 of 95 posts
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#32also 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
#33Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#34Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#35Cool. Now why doesn't PHP join the crowd and do this with arrays already?
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#36Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#37also 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.
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#38Earlier 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.
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#39Earlier 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.
% 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
#40also 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?
>> a = :foobar
=> :foobar
>> {a => 1}
=> {:foobar=>1}