Ruby Hash Equivalent of JavaScript's Object Initializer Es6 Shorthand

Ruby hash equivalent of JavaScript's object initializer ES6 shorthand

Update: will be in 3.1
https://bugs.ruby-lang.org/issues/14579#note-14

No, there is no such shorthand notation.

Related proposals:
#11105
#13137

Rejected. I saw JavaScript new syntax, but I had no sympathy. It doesn't make anything more understandable.

Matz.

How to convert an array of key-value tuples into an object

Update June 2020

ECMAScript 2021 brings Object.fromEntries which does exactly the requirement:

const array =    [ [ 'cardType', 'iDEBIT' ],
[ 'txnAmount', '17.64' ],
[ 'txnId', '20181' ],
[ 'txnType', 'Purchase' ],
[ 'txnDate', '2015/08/13 21:50:04' ],
[ 'respCode', '0' ],
[ 'isoCode', '0' ],
[ 'authCode', '' ],
[ 'acquirerInvoice', '0' ],
[ 'message', '' ],
[ 'isComplete', 'true' ],
[ 'isTimeout', 'false' ] ];

const obj = Object.fromEntries(array);
console.log(obj);

Checking if a key exists in a JavaScript object?

Checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually undefined?

var obj = { key: undefined };
console.log(obj["key"] !== undefined); // false, but the key exists!

JavaScript equivalent to printf/String.Format

Current JavaScript

From ES6 on you could use template strings:

let soMany = 10;
console.log(`This is ${soMany} times easier!`);
// "This is 10 times easier!"

See Kim's answer below for details.



Older answer

Try sprintf() for JavaScript.


If you really want to do a simple format method on your own, don’t do the replacements successively but do them simultaneously.

Because most of the other proposals that are mentioned fail when a replace string of previous replacement does also contain a format sequence like this:

"{0}{1}".format("{1}", "{0}")

Normally you would expect the output to be {1}{0} but the actual output is {1}{1}. So do a simultaneous replacement instead like in fearphage’s suggestion.

Why are symbols in Ruby not thought of as a type of variable?

Symbols used in accessor methods are not variables. They are just representing the name of a variable. Variables hold some reference, so you cannot use a variable itself in defining accessor methods. For example, suppose you wanted to define an accessor method for the variable @foo in a context where its value is "bar". What would happen if Ruby's syntax were to be like this:

attr_accessor @foo

This would be no different from writing:

attr_accessor "bar"

where you have no access to the name @foo that you are interested in. Therefore, such constructions have to be designed to refer to variable names at a meta level. Symbol is used for this reason. They are not variables themselves. They represent the name of a variable.

And the variable relevant to accessor methods are instance variables.

ListT.Contains not showing overload

Make sure to reference System.Linq and include it (using System.Linq) in your namespaces.

The second call you are referring to is actually Enumerable.Contains in System.Linq.


Edit in response to comments:

You could make your own version:

public void ContainedInList(IEnumerable<T> list, T value, IEqualityComparer<T> comparer)
{
foreach(T element in list)
{
if (comparer.Equals(element, value))
return true;
}
return false;
}


Related Topics



Leave a reply



Submit