This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Understanding Symbols In Ruby
What is the colon operator in Ruby?
I really feel naive asking this, but I'm going to go ahead and ask :
What is the importance of : in ruby ?
I have seen it being used in a number of places like params[:id] or like x < :length.
A colon denotes a "symbol". A symbol is like a string, but it is immutable (you can't change its contents). Behind the scenes, it also takes up less memory, since a symbol only needs to exist once in memory (i.e., two strings called "length" will exist twice in memory, but two symbols called :length will point to the same object).
:length means it is a Symbol
Symbols are Strings, just with an important difference, Symbols are immutable.
RubyDoc: Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods.
Here are some good places to learn more about symbols
The Ruby_Newbie Guide to Symbols
Understanding Ruby Symbols
The Difference Between Ruby Symbols and Strings
It is syntax indication of type for interpreter.
0-9 numeric*
: symbol
"" string
[] array
{} hash
Patterns more complicated in reality.
Related
Can someone please explain what (:+) is in Ruby? I have tried googling it & looking a reference guides and cant find anything. Thanks, sorry Im pretty new to Ruby & programming.
A colon : before a sequence of characters* is a Symbol literal. This applies to :+, which is a Symbol with content "+".
A symbol can be used to reference a method with the same name in some contexts, and in a couple of places your example :+ can be a reference to the + operator, which is really just a method with the same name. Ruby supports syntax to call it when it sees a plain + in an expression, or in some core methods it will convert :+
As an example you can use :+ as shorthand to create a sum of an Array of integers:
[1,2,3,4].inject( :+ )
=> 10
This works because Ruby has special-cased that specific use of operators in Array#inject (actually defined in Enumberable#inject, and Array gets it from that module).
A more general use-case for a symbol like this is the send method:
2.send( :+, 2 )
=> 4
Although 2.send( "+", 2 ) works just fine too. It might seem odd when used like this instead of just 2 + 2, but it can be handy if you want to make a more dynamic choice of operator.
* The rules for the syntax allowed or not allowed in a Symbol literal are a little arcane. They enable you to write shorter literals where possible, but Ruby has to avoid some ambiguous syntax such as a Symbol with a . or whitespace in the middle. This is allowed, just you have to add quotes if you generate such a Symbol e.g. :"this.that"
Ruby will tell you
:+.class
# Symbol
(:+) is the symbol in parentheses.
This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Closed 7 years ago.
So I was playing around in Ruby, and noticed that "a""b" returns "ab". If fond this very strange and useless, so I was wondering what this is called and if it has a purpose. I would appreciate any answers. Thanks!
This is called "string literal concatenation" and it is common in many languages. More specifically, adjacent string literals that are not separated by any other operators are automatically concatenated together. They may be considered to be just one string literal.
This exists in C, C++, Python, and Ruby to name a few.
MSDN: String Literal Concatenation (C)
Lexical Analysis: String literal concatenation (Python)
Where is Ruby's string literal juxtaposition feature officially documented? (Ruby)
Literals - Strings (Ruby)
An example of where this might be used is to break up a long string onto multiple lies, also adding the ability to comment each piece. Something I wrote in Python the other day:
hdr = struct.Struct('<'
'8s' # 0x00 Magic value
'I' # 0x08 Offset
'I' # 0x0C Length
'H' # 0x10 Type
'H' # 0x12 Flags
) # 0x14 (Total)
Note that this method takes just one parameter, a string, and I didn't manually concatenate the pieces.
I've never noticed this before, and this looks to be another form of concatenation like << and +.
This question already has answers here:
What are <-- Ruby Strings called? And how do I insert variables in them?
(3 answers)
Closed 8 years ago.
Can someone tell me the name of
<<-MAP
STRING HERE
MAP
operator (<<-) in ruby? I tried search for 'double less than' but it didn't turn up anything. I want to learn more about it but don't even know what it's called!
Thanks
Thats called the here doc syntax .Generally used to enter multiline strings. You can read about it here http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
and also here The <<- operator on Ruby, where is it documented?
It's not an operator, it's a here document (aka heredoc) String literal. It works more or less like heredocs in other languages.
It is specified in section 8.7.6.3.6 of the ISO Ruby Language Specification.
Is it possible to split a symbol without first converting it to a string? For example, I've tried
:split_this.split("_")
and it only returns an error. I've looked through the Symbol class reference, but all the example use to_s to convert it to a string.
I know I can convert it to a string, split it, and convert the two substrings to symbols, but that just seems a bit cumbersome. Is there a more elegant way to do this?
Since Ruby 1.9 some string's features are added to the Symbol class but not this much.The best you can do, I think is:
:symbol_with_underscores.to_s.split('_').map(&:to_sym)
You could turn this into a Symbol method:
class Symbol
def split(separator)
to_s.split(separator).map(&:to_sym)
end
end
:symbol_with_underscores.split('_')
# => [:symbol, :with, :underscores]
Think about symbols as numbers. Because symbols are internally stored as int numbers. Therefore they don't have string related methods.
I always do this to test string equality in Ruby:
if mystring.eql?(yourstring)
puts "same"
else
puts "different"
end
Is this is the correct way to do this without testing object equality?
I'm looking for the most concise way to test strings based on their content.
With the parentheses and question mark, this seems a little clunky.
According to http://www.techotopia.com/index.php/Ruby_String_Concatenation_and_Comparison
Doing either
mystring == yourstring
or
mystring.eql? yourstring
Are equivalent.
Your code sample didn't expand on part of your topic, namely symbols, and so that part of the question went unanswered.
If you have two strings, foo and bar, and both can be either a string or a symbol, you can test equality with
foo.to_s == bar.to_s
It's a little more efficient to skip the string conversions on operands with known type. So if foo is always a string
foo == bar.to_s
But the efficiency gain is almost certainly not worth demanding any extra work on behalf of the caller.
Prior to Ruby 2.2, avoid interning uncontrolled input strings for the purpose of comparison (with strings or symbols), because symbols are not garbage collected, and so you can open yourself to denial of service through resource exhaustion. Limit your use of symbols to values you control, i.e. literals in your code, and trusted configuration properties.
Ruby 2.2 introduced garbage collection of symbols.