Strings In Ruby

String is nothing but an object that holds a arbitrary sequence of bytes which typically represents characters.

The string .new is used to create new objects of strings.

The Secret of the Exclamation Mark(!)

Ex: Delete and Delete!

-The Method with ! mark tend to modify the behaviour of the receiver.

-Those method without a ! mark tend to return a new string as an output.

Mixins For Ruby

Comparables:

<, <=, ==, >=, >, between?

Enumarable: collect, detect, each_with_index, entries, find, find_all, grep, include?, map, max, member?, min, reject, select, sort, to_a

String Multiplication

Strings in ruby do understand Multiplication.

But don’t compare this with addition and division because they might throw errors

In case of the %( Division sign) the string evaluates “Hello Ruby” as a format specification. Since its not able to find any format specification of its kind in Kernel: sprintf class method, it tends to ignore the % 3 argument as directly displays the “Hello Ruby”.

Element Reference And Element Assignment In Strings

This comes into play when you want to insert or substitute a string with some kind of special characters, values or other string.

The element referencing is based on the offset and the length.

The range and length combination can also be used for element referencing

Secure your strings using crypt

The string class provides the crypt method for crypting a string and mainly used for secure data transmission.

Its takes an argument as a salt string which should be minimum 2 characters long

Dump of a String

The dump method produces the version of the string with all non printing characters replaced by \ notation and all special characters escaped.

Splitting your strings using each

each” splits your string based on the input parameter and then supplies the substring to the evaluating block.

Syntax:

string”.each (parameter){|substring| play with substring}

 

When we pass the no parameters in the first case, we can see that the string has been passed as it is.

In the second case the string is substring is created wherever it finds the letter “e”

In the last scenario the string is evaluated wherever its finds the newline character.

Scanning of a String

The scan function tends to parse the string based on a matching pattern (which is RegEx) .

The result generated is either added to an array or may be passed to the evaluating block.

 

Splitting the String

The scan divides the string into two sub strings and passes these substrings as a result to an array.

The difference between split and scan is split takes an input parameter called “limit”.The limit is generally a positive number which divides the string into the substrings based on the limit.

 

In the first case, when no parameters are passed to the split method, it tends split the string wherever it finds the whitespace.

In the second case,we are splitting the string at every character that it comes across.

In the third scenario,we pass the limit of 3 as the second parameter so, it will parse and break the string into exact 3 division of its own as shown in the example.

Squeezing the strings

The squeeze method tends to build a new string string by elimination of same set of characters that occur in the string and replacing them with a single character of its same kind.

 

Swapcasing your Strings

The swapcase tends to convert the string characters with upcase to downcase and vice versa.

 

String Interpolation

The word interpolation sounds pretty scary, but interpolation is nothing but the ability to insert some kind of value, like a variable or some kind of code snippet into the string.

Double quoted strings allow using #{…} notation for insertion of evaluated values within the string.

In the second case you can escape the brackets for instance, class and Global variables.

But in case of local variables you cannot skip those braces as you can see the output has failed to resolve the local variable name.

The Q Factor in strings

Ruby allows you to create string using %q and %Q followed by delimiter of your choice.

These are mainly used when your string has many single quotes as well as double quotes which will definitely bug you if you try to escape them using the \ .

Here %q is equivalent to single quote and %Q is equivalent to the double quote

Difference between sub and gsub

The main difference between these two instance methods comes in focus when regular expressions are mingled with your strings.

As we can in the example, sub tends to substitute only for the first match where as gsub will substitute for all the matches found within the string.

Difference between capitalize and upcase

Capitalize returns a string with the first character converted to uppercase and the remaining to the lowercase where as the upcase converts all the characters in the strings to the upper case.

Difference between chomp and chop

chomp” returns a new string with a given record separator removed from end of the string if present.

chop” returns a new string with the last character removed.


Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • blogmarks
  • Book.mark.hu
  • co.mments
  • Technorati
  • YahooMyWeb
  • IndianPad
  • DZone