|
From: | Dmitry Gutov |
Subject: | Re: Guidelines for the "symbol" syntax class |
Date: | Mon, 4 Jan 2016 04:34:04 +0200 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Thunderbird/43.0 |
On 01/04/2016 04:21 AM, John Yates wrote:
On Sun, Jan 3, 2016 at 8:35 PM, Dmitry Gutov <address@hidden <mailto:address@hidden>> wrote: > > => [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, ...] It seems similar to C++'s operator keyword. Am I getting closer? Is the colon required to abut the subsequent characters or can one write : <=> (note intervening space)?
No space allowed. See this usage example: irb(main):003:0> "abc" == "def" => false irb(main):004:0> String.instance_method(:==) => #<UnboundMethod: String#==> irb(main):005:0> "abc".size => 3 irb(main):006:0> String.instance_method(:size) => #<UnboundMethod: String#size> irb(main):007:0> "abc".method(:size) => #<Method: String#size> irb(main):008:0> "abc".method(:size).call => 3You can have Symbols with any name, though. So they are not tied to methods, variables or anything.
Here's a good explanation: http://www.randomhacks.net/2007/01/20/13-ways-of-looking-at-a-ruby-symbol/, in particular, comparison #6 rings true: Ruby Symbols are similar to Lisp symbols. In Lisp, one can reference a dynamically bound variable, or call a function, using a symbol:
(let ((sym 'car)) (funcall sym nil)) or even (let ((sym 'car)) (funcall (symbol-function sym) nil)) which is similar to "abc".method(:size).call I've tried above.
[Prev in Thread] | Current Thread | [Next in Thread] |