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

9 Comments:

Blogger Unknown 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 शंतनू 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 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 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 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 Unknown 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 Unknown 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 Unknown 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

<< Home