General Flashcards
Based on information from http://www.zenspider.com/Languages/Ruby/QuickRef.html
What is a comment in Ruby code?
from # to end of line
A Ruby program is a sequence of…
expressions
How do you terminate an expression?
with a ; or end-of-line
Can an expression go beyond end-of-line?
Yes; if it is parsed as incomplete, parsing continues on next line
Is there a way to force an expression to run past end-of-line unconditionally?
Yes, by ending the line with a backslash \
What are the basic Ruby data types?
numbers, strings, ranges, regexes, symbols, arrays, hashes (and files?)
Can a large integer be broken up for readability?
Yes, with underscores, e.g. 1_000_000
How is a literal hexadecimal value denoted?
With “0x”, C style, e.g. 0xffff
How is a floating-point value expressed
C style, e.g. 1.2e-3
How is a literal octal value expressed
With “0” prefix, C style, e.g. 0377
How is a literal binary value expressed?
With “0b”, e.g. 0b01011
How is the literal value of an ASCII character expressed?
With a “?” prefix and no quotes, e.g. ?a
How is the literal value of an ASCII control character expressed?
Prefixed with “?\C-“, e.g. ?\C-a
How is the literal value of a “meta” character expressed?
Prefixed with “?\M-“, e.g. ?\M-a
What is the simplest way to express strings (non-interpolated, no escaping)?
Single quoted, e.g. ‘string’
What is the simplest way to express an interpolated string?
Double quoted, e.g. “value of #{variable}”
Can an interpolated string use C-style backslash characters?
Yes (e.g. “\n” will be a newline)
Can a non-interpolated string use C-style backslash characters?
No (e.g. ‘\n’ will literally be backslash + n)
How else can a non-interpolated string be quoted?
%q(string)
How can an interpolated string be quoted without quote marks?
%Q(string) or %(string)
What is the simplest way to make a string a command to be executed in the shell?
Quoted with backticks, e.g. cat /etc/passwd
How else can a shell-command string be quoted
%x(echo Hello)
Can %-style strings be quoted any other way besides parentheses?
Yes, with matching pairs e.g. %q[string], %Q{string}, %x<echo string>; or any single char twice, e.g. %@string@, %Q!string!, %x,echo string,