Tuesday, June 27, 2006

Strings in Ruby

Let us explore Strings in Ruby. We refer to a group of letters in a program as strings. Strings can have punctuation, digits, symbols, and spaces in them...more than just letters. '' does not have anything in it at all; we call that an empty string. Here's the program RubyStrings.rb
=begin
Ruby Strings
=end

puts "Hello World"
# Can use " or ' for Strings, but ' is more efficient
puts 'Hello World'
# String concatenation
puts 'I like' + ' Ruby'
# Escape sequence
puts 'It\'s my Ruby'
# New here, displays the string three times
puts 'Hello' * 3
# Defining a constant
PI = 3.1416
puts PI
# Defining a local variable
myString = 'I love my city, Pune'
puts myString
=begin
Conversions
.to_i, .to_f, .to_s
=end
var1 = 5;
var2 = '2'
puts var1 + var2.to_i
Questions asked by the participants:
1. Sharif Kazi - Please explain how memory is managed for Strings in Ruby? Is there a separate pool for Strings?
Answer: Strings are objects of class String. The String class has more than 75 standard methods. If you refer to Ruby User's Guide, it says that "we do not have to consider the space occupied by a string. We are free from all memory management."

2. Anish - It's to be noted that any given variable can at different times hold references to objects of many different types. - Does that mean a variable of type x can hold reference to the variable of type b without direct relation (inheritance) between them?
Answer: Please go thro' the code in the program DiffObjects.rb We can assign an object to a variable. Also, we can reassign a different object to that variable. In fact, variables can point to just about anything...except other variables. So in the program, when we tried var2 = var1, it really pointed to 8 instead. Then with var1 = 'eight', we had var1 point to the string 'eight', but since var2 was never really pointing at var1, it stays pointing at the number 8.

3. Deepali - Do we have something like print of Java?
Answer: Yes, we do have that in Ruby. Write a Ruby program that has this statement and check the output.
STDOUT.print("This is a string")


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

15 Comments:

Blogger SatishTalim said...

Here's the output:
>ruby RubyStrings.rb
Hello World
Hello World
I like Ruby
It's my Ruby
HelloHelloHello
3.1416
I love my city, Pune
7
>Exit code: 0

10:52 AM  
Blogger sharifkazi said...

How memory is managed for Strings in Ruby? Is there any separate pool for Strings?

11:55 AM  
Blogger Vikrant Chaudhari said...

What is dynamic programming language?

12:31 PM  
Blogger शंतनु said...

What exactly is '$-'. If I declare a variable 'x', and later on I want to make it global, can I do that?

1:47 PM  
Blogger DeepaliAhirrao said...

Every puts statement works like println in Java i.e prints on a new line. Is there something like print?

2:53 PM  
Blogger Ashish Kulkarni said...

I have some more questions....which I have published on my blog http://ashishkulkarni.blogspot.com.

I would like to know how we can read stuff from the console into variables....and more.

2:54 PM  
Blogger Tejaswini Patwardhan said...

Sir, How to print the string with integer value? like in java, we can print hello 100 as 'hello' +100
but in ruby, hoe to do that?

4:15 PM  
Blogger Ashish Kulkarni said...

Tejaswini,

The answer to that one would be:

puts "Hello" + 100.to_s

I have been hunting for some answers to my ever-increasing questions and have found printf and gets functions.

printf does formatted printing and gets reads a line from the console.

I have put the examples on my blog: http://ashishkulkarni.blogspot.com

Regards,

Ashish.

7:01 PM  
Blogger manoj sagar said...

sir what is =begin
=end

is it used to define variables or expressions

7:48 PM  
Blogger Ashish Kulkarni said...

Manoj,

That is for defining comments spanning multiple lines. It is similar to the /* ... */ of C, C++ and Java.

Regards,

Ashish.

11:32 PM  
Blogger bitsmithusa said...

what does
=begin
Ruby Strings
=end
at the begining and at the end do?

1:30 AM  
Blogger SatishTalim said...

Manoj and Dave, as Ashish mentions in his comment -
=begin
=end
is used for defining comments spanning multiple lines. It is similar to the /* ... */ of C, C++ and Java.

I have mentioned this in my blog post " Some features of Ruby"

4:43 AM  
Blogger SatishTalim said...

Shantanu, to answer part of your question -
Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x).

4:55 AM  
Blogger शंतनु said...

A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x).

do you mean to say '$' or '$-'? Or is - belongs to [a-zA-z_][a-zA-Z0-9_]*

5:17 PM  
Blogger SatishTalim said...

Shantanu, I said that -
Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x)
Name characters: any combination of upper- and lowercase letters, underscore and digits.

5:21 PM  

Post a Comment

Links to this post:

Create a Link

<< Home