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
Xmlhttprequest Status 0 (Responsetext Is Empty)
Sort an Array of Object by a Property (With Custom Order, Not Alphabetically)
How Should Look a Application.Scss File in Ruby
Console.Log() After Setstate() Doesn't Return the Updated State
What Does It Mean That JavaScript Is a Prototype Based Language
Which Ecmascript 6 Features Imply Strict Mode
How to Force Reloading a Page When Using Browser Back Button
Declaring Static Constants in Es6 Classes
Use Browser Search (Ctrl+F) Through a Button in Website
How to Set Bootstrap Navbar Active Class with Angular Js
Rails 4 Client Side Validation
How to Return a Variable from Google Maps JavaScript Geocoder Callback
React - User-Defined Jsx Components Not Rendering
How to Wait in Node.Js (Javascript)? L Need to Pause for a Period of Time
No-JavaScript Detection Script + Redirect
Improving Performance of Click Detection on a Staggered Column Isometric Grid