Create array of symbols
The original answer was written back in September '11, but, starting from Ruby 2.0, there is a shorter way to create an array of symbols! This literal:
%i[address city state postal country]
will do exactly what you want.
Using %i and %I symbol array literal
I'm having trouble finding what the
%i
does in relation to a symbol array.
It is an array literal for an array of symbols. It does the same thing in relation to symbol arrays as '
does to strings.
Is there a literal notation for an array of symbols?
Yes! This is possible now in Ruby 2.0.0. One way to write it is:
%i{foo bar} # => [:foo, :bar]
You can also use other delimiters, so you could also write %i(foo bar)
or %i!foo bar!
for example.
This feature was originally announced here:
http://www.ruby-lang.org/zh_TW/news/2012/11/02/ruby-2-0-0-preview1-released/
It is mentioned in the official documentation of Ruby here:
http://ruby-doc.org/core/doc/syntax/literals_rdoc.html#label-Percent+Strings
How can I add operation symbols (+, *, -, /) to an array in Javascript
You simply need to add quote around the symbols as follows -
var operations = ['*', '+', '-', '/'];
console.log(operations);
function getRandom(){
return operations[Math.floor(Math.random() * Math.floor(4))]
}
// To get a random symbol -
console.log(getRandom())
// One way to use a random symbol in a given expression would be as -
const num1 = Math.floor(100*Math.random()), num2 = Math.floor(100*Math.random());
const expression = `${num1}${getRandom()}${num2}`
const result = eval(expression)
console.log(result)
Using an array of strings to implement a symbol table in C
- Use descriptive names for identifiers, not cryptic short names (like
s
andstr_id
). - Avoid Systems Hungarian Notation (i.e. naming or prefixing identifiers after their type or what-they-are as opposed to what-they-mean).
- In your case, I assume
str_id
is an abbreviation forstruct_id
(orstring_id
) - which is a bad name because it's already immediately obvious that it's astruct
(or contains a string). - It was popular right until the 1990s when programmers started using more powerful editors and IDEs that kept track of variable types - it just isn't needed today. *
- In your case, I assume
- Always check if a heap allocation succeeded or failed by comparing
calloc
andmalloc
's return values toNULL
. This can be done withif( some_pointer ) abort()
.- Don't use
assert( some_pointer )
because assertions are only enabled in debug builds, useabort
instead as it signifies abnormal program termination compared toexit
.
- Don't use
- Pass a
size_t
parameter so consumers can specify the size of the symbol table. - Quantities of objects held in memory should be expressed as
size_t
(e.g. array indexers). Never useint
for this! - You need to put a semi-colon at the end of each
struct
definition. - Are you sure you want an
array-of-pointers-to-structs
and not just anarray-of-structs
? In this case you can use inline structs and use a single allocation for the array, instead of allocating each member separately. - Because you're performing custom allocation, you must also define a destructor function.
struct symbol_table_entry {
char* symbolText;
int id;
};
struct symbol_table {
size_t count;
struct symbol_table_entry** entries;
};
struct symbol_table* create_symbol_table( size_t count ) {
struct symbol_table* stt = malloc( sizeof(struct symbol_table) );
if( !stt )
{
abort();
}
stt->count = count;
stt->entries = calloc( count, sizeof(struct symbol_table_entry) );
if( !stt->entries ) {
free( stt );
abort();
}
// Note that calloc will zero-initialize all entries of the array (which prevents debuggers showing garbage string contents) so we don't need to do it ourselves.
return stt;
}
void destroy_symbol_table( struct symbol_table* stt, bool free_strings ) {
if( stt->entries ) {
if( free_strings ) {
for( size_t i = 0; i < stt->count; i++ ) {
free( stt->entries[i]->symbolText );
}
}
free( stt->entries );
}
free( stt );
}
Create array of symbols with the length of split string
The problem is in this line:
for palabra_a_jugar in range(0,len(palabra_a_jugar.split())):
Array_Palabra.append("_")
palabra_a_jugar
starts out as string. split()
returns a list of substrings, broken at a delimiter, which is whitespace by default. So
'palabara a jugar'.split()
would return ['palabra', 'a', 'jugar']
However, you have a single word, presumably. Say that the value of palaba_a_jugar
is "pretenciosas". Then there's no whitespace, so
'palabara a jugar'.split()
will return
['pretenciosas']
a one-element list. This explains the behavior you're seeing.
However, you don't need to go all around the barn like this, and it isn't really worth writing a function for it. A list of stars the same length as palabra_a_jugar
is just
len(palabra_a_jugar) * ['*']
For example, 4*['*']== ['*','*','*','*']
Related Topics
On Destroying Session via Devise "Couldn't Find User with 'Id'=Sign_Out"
Rspec: How to Write Unit Test Case to Receive an Exception Which Is Getting Raised in Private Method
Ruby Devise, Sessionscontroller.Create, JSON - Getting Nameerror: Undefined 'Build_Resource'
Render Erb from Database into View Problem Please Help!
Rails 3 - Has_And_Belongs_To_Many
What Ruby Features Are Used in Chef Recipes
Net::Ssh with Non Unix/Linux Host
Ruby Check Whether Program Is Currently Being Closed
Understanding Ruby Method Parameters Syntax
Why Won't Ternary Operator Work with Redirect
Cannot Require 'Nokogiri' in Rails (But Works in Irb)
Gracefully Shutting Down Sidekiq Processes
Modern Tools for Ruby/Rails for Building an Achievement System
Cocoapods Not Working in MACos High Sierra
Get File Name and Extension in Ruby
Possible to Access the Index in a Hash Each Loop
Why Are Database Entries Being Automatically Created When I Visit the "New" Page