This question already has answers here:
Why do two strings separated by space concatenate in Ruby?
(2 answers)
Closed 8 years ago.
How the following line of code concatenates a string in ruby?
2.1.0 :052 > value = "Kamesh" "Waran"
=> "KameshWaran"
I understand that '+' is a method on String class which concatenates the strings passed. How the space(' ') can be the operator/method?
Can anyone elaborate how the space(' ') concatenate strings?
The space is not an operator. This only works for string literals, and is just part of the literal syntax, like the double-quotes. If you have two string literals with nothing but whitespace between them, they get turned into a single string. It's a convention borrowed from later versions of C.
irb(main):001:0> foobar = "foo" "bar"
=> "foobar"
irb(main):002:0> foo="foo"
=> "foo"
irb(main):003:0> bar="bar"
=> "bar"
irb(main):004:0> foo bar
NoMethodError: undefined method `foo' for main:Object
from (irb):4
from /usr/local/var/rbenv/versions/2.1.3/bin/irb:11:in `<main>'
irb(main):005:0>
If you do a search on this site you get an answer.
Found on :
Why do two strings separated by space concatenate in Ruby?
Implementation details can be found in parse.y file in Ruby source
code. Specifically, here.
A Ruby string is either a tCHAR (e.g. ?q), a string1 (e.g. "q", 'q',
or %q{q}), or a recursive definition of the concatenation of string1
and string itself, which results in string expressions like "foo"
"bar", 'foo' "bar" or ?f "oo" 'bar' being concatenated.
Related
This question already has answers here:
what is "?" in ruby
(3 answers)
Closed 3 years ago.
I ran across a code snippet today that used the ? operator to quote the next character. I have no idea where the documentation is for this method and really no idea what it's actually doing.
I've looked at the ruby docs, but haven't found it.
?1
=> "1"
?1"23abc"
=> "123abc"
? is not a method in this case but rather a parsable syntax. ? is a character literal in this context
Docs Excerpt:
There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding:
?a #=> "a"
?abc #=> SyntaxError
?\n #=> "\n"
?\s #=> " "
?\\ #=> "\\"
?\u{41} #=> "A"
?\C-a #=> "\x01"
?\M-a #=> "\xE1"
?\M-\C-a #=> "\x81"
?\C-\M-a #=> "\x81", same as above
?あ #=> "あ"
You have also found another fun little mechanism of the parser which is 2 Strings can be concatenated together by simply placing them side by side (with or without white space). e.g.
"1" "234"
#=> "1234"
"1""234"
#=> "1234"
Why does this work in Ruby:
"foo" "bar"
# => "foobar"
I'm unsure as to why the strings were concatenated instead of a syntax error being given.
I'm curious as to whether or not this is expected behavior and whether or not it's something the parser is responsible for wrangling (two strings without operators is considered a single string) or the language definition itself is specifying this behavior (implicit concat).
In C and C++, string literals next to each other are concatenated. As these languages influenced Ruby, I'd guess it inherits from there.
And it is documented in Ruby now: see this answer and this page in the Ruby repo which states:
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion" #=> "concatenation"
"This string contains "\
"no newlines." #=> "This string contains no newlines."
Any combination of adjacent single-quote, double-quote, percent strings will be concatenated as long as a percent-string is not last.
%q{a} 'b' "c" #=> "abc"
"a" 'b' %q{c} #=> NameError: uninitialized constant q
Implementation details can be found in parse.y file in Ruby source code. Specifically, here.
A Ruby string is either a tCHAR (e.g. ?q), a string1 (e.g. "q", 'q', or %q{q}), or a recursive definition of the concatenation of string1 and string itself, which results in string expressions like "foo" "bar", 'foo' "bar" or ?f "oo" 'bar' being concatenated.
This question already has an answer here:
Why does string interpolation work in Ruby when there are no curly braces?
(1 answer)
Closed 8 years ago.
Please observe the following:
"abcd#fg" # => "abcd#fg"
"abcd#$fg" # => "abcd" characters #$ and after them are skipped
"abcd##fg" # => "abcd" characters ## and after them are skipped
It could be string interpolation just with # instead of #{}.
$fg = 8
"abcd#$fg" # => "abcd8"
#fg = 6
"abcd##fg" # => "abcd6"
It works like interpolation. Is it a bug or a feature?
You can actually interpolate global, instance, and class variables omitting the braces:
$world = 'world'
puts "hello, #$world"
# hello, world
In your example both $fg and #fg are uninitialized and thus evaluated to nil, that's why they are intorpolated as empty strings. When you write "abcd#fg" nothing is interpolated because the # is not followed by one of {, #, $.
You can find the feature documented in the RubySpec (thanks to #DavidMiani).
If you ask me, don't rely on this behaviour and always interpolate variables using braces, both for readability and to avoid problems such:
#variable = 'foo'
puts "##variable_bar"
This will output an empty string instead of the, probably, expected string "foo_bar", because it is trying to interpolate the undefined instance variable #variable_bar.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the colon operator in Ruby?
While learning Ruby I've come across the ":" operator on occasion. Usually I see it in the form of
:symbol => value
what does it mean?
It just indicates a that it is a symbol instead of a string. In ruby, it is common to use symbols instead of strings.
{:foo => value}
{'foo' => value}
It's basically a short-hand way of expressing a string. It can not contain spaces as you can imagine so symbols usually use underscores.
Try this on your own:
foo = :bar
foo.to_s # means to string
baz = 'goo'
baz.to_sym # means to symbol
I'm reading some Ruby code and I don't understand this snippet:
thing = '${other-thing}/etc/'
It appears to substitute a value for the ${other-thing} and use that to build the String thing but I haven't been able to recreate this myself.
EDIT: Sorry to all, it turns out there was some preprocessing going on by Maven (a Java build tool). The accepted answer shows how one could do the substitution in straight Ruby.
$ irb
irb(main):001:0> a = "Hello"
=> "Hello"
irb(main):002:0> b = "world"
=> "world"
irb(main):003:0> puts "${a}, ${b}!" # Doesn't work.
${a}, ${b}!
=> nil
irb(main):004:0> puts "#{a}, #{b}!" # Works fine.
Hello, world!
=> nil
irb(main):005:0> puts '#{a}, #{b}!' # Doesn't work.
#{a}, #{b}!
=> nil
You wanted #{...}, not ${...} I believe. Also, you don't get substitutions inside of single-quoted strings, only double-quoted (or equivalents – there's dozens of ways to delimit strings in Ruby).