# Concatenation, with a space in between # The equivalent interpolation in Java

Drawer Data Matrix in Java # Concatenation, with a space in between # The equivalent interpolation
# Concatenation, with a space in between # The equivalent interpolation
DataMatrix Printer In Java
Using Barcode creation for Java Control to generate, create Data Matrix ECC200 image in Java applications.
Note that the final two expressions are equivalent, but I prefer the interpolated version; having to add the single space " " seems a bit awkward
Painting Bar Code In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
Printing To print a string, the most commonly used Ruby function is puts (pronounced put ess , for put string ):
Barcode Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
>> puts "foo" foo => nil # put string
Printing Data Matrix In C#
Using Barcode generation for .NET framework Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
The puts method operates as a side-effect: the expression puts "foo" prints the string to the screen and then returns literally nothing: nil is a special Ruby value for nothing at all (In what follows, I ll sometimes suppress the => nil part for simplicity)
ECC200 Drawer In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
7 Programmers familiar with Perl or PHP should compare this to the automatic interpolation of dollar sign variables in expressions like "foo $bar"
Painting ECC200 In .NET
Using Barcode drawer for VS .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
4: Rails-Flavored Ruby
Creating ECC200 In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
Using puts automatically appends a newline character \n to the output; the related
Bar Code Generator In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
print method does not:
Data Matrix ECC200 Drawer In Java
Using Barcode generation for Java Control to generate, create DataMatrix image in Java applications.
>> print "foo" foo=> nil >> print "foo\n" foo => nil
Printing Barcode In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
# print string (same as puts, but without the newline) # Same as puts "foo"
GS1 - 13 Encoder In Java
Using Barcode drawer for Java Control to generate, create European Article Number 13 image in Java applications.
Single-Quoted Strings All the examples so far have used double-quoted strings, but Ruby also supports singlequoted strings For many uses, the two types of strings are effectively identical:
Draw Code39 In Java
Using Barcode generation for Java Control to generate, create Code 39 Full ASCII image in Java applications.
>> => >> => 'foo' "foo" 'foo' + 'bar' "foobar" # A single-quoted string
USPS POSTNET Barcode Maker In Java
Using Barcode encoder for Java Control to generate, create Delivery Point Barcode (DPBC) image in Java applications.
There s an important difference, though; Ruby won t interpolate into single-quoted strings:
Code 128A Printer In .NET
Using Barcode drawer for ASP.NET Control to generate, create USS Code 128 image in ASP.NET applications.
>> '#{foo} bar' => "\#{foo} bar"
Barcode Printer In Visual C#
Using Barcode printer for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.
# Single-quoted strings don't allow interpolation
EAN-13 Printer In .NET Framework
Using Barcode generation for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
Note how the console returns values using double-quoted strings, which requires a backslash to escape characters like # If double-quoted strings can do everything that single-quoted strings can do, and interpolate to boot, what s the point of single-quoted strings They are often useful because they are truly literal, and contain exactly the characters you type For example, the backslash character is special on most systems, as in the literal newline \n If you want a variable to contain a literal backslash, single quotes make it easier:
Bar Code Maker In Visual Studio .NET
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in .NET applications.
>> '\n' => "\\n"
UPC-A Supplement 5 Generation In Visual Studio .NET
Using Barcode creator for .NET framework Control to generate, create GTIN - 12 image in VS .NET applications.
# A literal 'backslash n' combination
Read Bar Code In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
Strings and Methods
UPC-A Supplement 5 Recognizer In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
As with the # character in our previous example, Ruby needs to escape the backslash with an additional backslash; inside double-quoted strings, a literal backslash is represented with two backslashes For a small example like this, there s not much savings, but if there are lots of things to escape it can be a real help:
>> 'Newlines (\n) and tabs (\t) both use the backslash character \' => "Newlines (\\n) and tabs (\\t) both use the backslash character \\"
423 Objects and Message Passing
Everything in Ruby, including strings and even nil, is an object We ll see the technical meaning of this in Section 442, but I don t think anyone ever understood objects by reading the definition in a book; you have to build up your intuition for objects by seeing lots of examples It s easier to describe what objects do, which is respond to messages An object like a string, for example, can respond to the message length, which returns the number of characters in the string:
>> "foobar"length => 6 # Passing the "length" message to a string
Typically, the messages that get passed to objects are methods, which are functions defined on those objects8 Strings also respond to the empty method:
>> => >> => "foobar"empty false ""empty true
Note the question mark at the end of the empty method This is a Ruby convention indicating that the return value is boolean: true or false Booleans are especially useful
8 Apologies in advance for switching haphazardly between function and method throughout this chapter; in Ruby, they re the same thing: all methods are functions, and all functions are methods, because everything is an object
4: Rails-Flavored Ruby
for control flow :
>> >> >> >> >> >> => s = "foobar" if sempty "The string is empty" else "The string is nonempty" end "The string is nonempty"
Booleans can also be combined using the && ( and ), || ( or ), and ! ( not ) operators:
>> x = "foo" => "foo" >> y = "" => "" >> puts "Both strings are empty" if xempty && yempty => nil >> puts "One of the strings is empty" if xempty || yempty "One of the strings is empty" => nil >> puts "x is not empty" if !xempty "x is not empty"
Since everything in Ruby is an object, it follows that nil is an object, so it too can respond to methods One example is the to_s method that can convert virtually any object to a string:
>> nilto_s => ""
This certainly appears to be an empty string, as we can verify by chaining the messages we pass to nil:
>> nilempty NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array The error occurred while evaluating nilempty >> nilto_sempty # Message chaining => true