Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3|Last
Lessons:- Strings and Methods

Strings

Strings are probably the most important data structure for web applications, since web pages ultimately consist of strings of characters sent from the server to the browser.
Let’s start exploring strings with the console, this time started with rails c, which is a shortcut for rails console:

$ rails c
>> ""               # An empty string
=> ""
>> "foo"           # A nonempty string
=> "foo"

 These are string literals (also, amusingly, called literal strings), created using the double quote character ". The console prints the result of evaluating each line, which in the case of a string literal is just the string itself.
         We can also concatenate strings with the + operator:

>> "foo" + "bar" # String concatenation
=> "foobar"

 Here the result of evaluating "foo" plus "bar" is the string "foobar".2

Another way to build up strings is via interpolation using the special syntax #{}:3

>> first name = "Michael"    # Variable assignment
=> "Michael"
>> "#{first name} Hartl"     # String interpolation
=> "Michael Hartl"

Here we’ve assigned the value "Michael" to the variable first_name and then interpolated it into the string "#{first_name} Hartl". We could also assign both
strings a variable name:

 

Note that the final two expressions are equivalent, but I prefer the interpolated version; having to add the single space " " seems a bit awkward.

Printing

To print a string, the most commonly used Ruby function is puts (pronounced ‘‘put ess,’’ for ‘‘put string’’):

>> puts "foo" # put string
foo
=> nil

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.) 

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)