Tuesday, 19 April 2011

Operations on String :Ruby Way

I  want this post to filled with examples. But since i don't have much examples i urges you to please post some examples via your precious comments and help this post to grow more and more.

Well, more the merrier. 


To generate Timestamp like this :  20110419230024



ruby-1.9.2-p0 > Time.now.strftime("%Y%m%d%H%M%S")
 => "20110419230024"

String Replace:  "gsub":
ruby-1.9.2-p0 > "just another sahils's stuff".gsub("stuff","crap")
 => "just another sahils's crap" 


String Concatenation
To concatenate string:
using "+" operator
"sahil" + "kewl" => "sahilkewl"

Using HereDocs
myString = <<LINEEND
My multiline string goes here
I can add as many line as i want.
This one is line :#{2+1}
LINEEND
 => "My multiline string goes here\nI can add as many line as i want.\nThis one is line :3\n"






No comments:

Post a Comment