Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Topics

Recent Status Updates

View All Updates

- - - - -

Ruby, More About The Language

ruby variable type

  • Please log in to reply
No replies to this topic

#1 RhetoricalRuvim


    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 August 2011 - 10:01 PM

Besides what we went over so far, Ruby also has operators and other methods.

There are also classes for the basic variable types. There is the Integer class, the String class, the Float class; each of those classes has methods (or functions).

Like to get from a float to a string, you would use the_float.to_s; in this case, the .to_s() method converts self to a string.

  • Common Ruby Operators
  • The Math Object
  • Some Common Methods
  • Example Program

I'll come back to the self keyword later.

Common Ruby Operators
To use an operator, <operator>, on a and b, you type this:
[COLOR=#FF4433]a[/COLOR] [COLOR=#FF2288]<operator>[/COLOR] [COLOR=#FF3344]b[/COLOR]

So to assign the result to a variable, d, you would type:
[COLOR=#55AA22]d[/COLOR]= [COLOR=#FF4433]a[/COLOR] [COLOR=#FF2288]<operator>[/COLOR] [COLOR=#FF3344]b[/COLOR]

Here are some common operators:
[COLOR=#FF2288]**[/COLOR]	[COLOR=#FF4433]a[/COLOR] to the [COLOR=#FF3344]b[/COLOR] power
[COLOR=#FF2288]*[/COLOR]	multiply [COLOR=#FF4433]a[/COLOR] by [COLOR=#FF3344]b[/COLOR]
[COLOR=#FF2288]/[/COLOR]	divide [COLOR=#FF4433]a[/COLOR] by [COLOR=#FF3344]b[/COLOR]
[COLOR=#FF2288]+[/COLOR]	add, to [COLOR=#FF4433]a[/COLOR], [COLOR=#FF3344]b[/COLOR]
[COLOR=#FF2288]-[/COLOR]	subtract, from [COLOR=#FF4433]a[/COLOR], [COLOR=#FF3344]b[/COLOR]

For example,
4 * 2.5
would evaluate to

You can also use operators on variables:
a= 4 
b= 2.5 
c= a * b

`c` should end up being 10

Besides these operators, there's also the Math object (or class).

The Math Object
The Math object provides us with some math methods and constants.

The constants are:

would evaluate to pi (3.14159265...).

would evaluate to e (2.71828...).

Some of the methods are:

These are just some of the methods that the Math method provides.

For more information, you can Google search "ruby math class"

To call a Math method, method, with parameter parameter, you type this:
Math.[COLOR=#C040A0]method[/COLOR] [COLOR=#40A0C0]parameter[/COLOR]

For example, this code:
Math.sqrt 4
will return

Why 2.0 and not 2? The square root of a number is rarely an integer, so the sqrt() method returns a float (a floating-point number; a type of number with a decimal point).

cbrt stands for "cube root," by the way.

Some Common Methods
There are some other methods that Ruby has.

The self keyword - if you're familiar with JavaScript or C++ or another language like that - is the Ruby version of the this keyword. It refers to the instance of the current object that's being used.

For example, let's look at the Integer class; let's make an increment function, so that this code:
a= 3 
would make `a` equal to 4 (first set it to 3 and then increment thatk, or add 1 to that).

We'll need to define the Integer class:
class Integer 
	def inc 
		self + 1 

What we did is we added the method `inc` to the class `Integer` .

would make `self` be 7, so the return would be 7 + 1 = 8

We can also, later in the code, redefine any function in any class:
class Integer 
	def inc 
		return self + 1 

What? Return? Doesn't it already return `self + 1` ? Yes, it does, but Ruby also has the return keyword, which can be used, in a function, to return extra early from the call.

For example, this function:
def do_something 
	puts 'hello' 
	return true 
	puts 'hi' 

would not print "hi\r\n", ever, because it would return before it even gets to that line. It would print "hello\r\n" and return TRUE.

In general, for the String, Integer, and Float classes, .to_s() converts the value to a string, .to_i() converts the value to an integer, and .to_f() converts the value to a floating-point number.

Some String Case Methods
.capitalize	capitalized string
.upcase	upper-cased string
.downcase	lower-cased string
.swapcase	swapped cases of each character

There's also the .chomp() method, which takes out the trailing "\r" character, if one exists (useful for getting keyboard input).

Example Program

The Code
# Ask the user for their name. 
puts "What is your name? " 
# Get user input. If we just use 'gets' then that would 
# include the "\r" return character, so we use 'gets.chomp' 
# The .chomp() method takes out the trailing "\r" return 
# character, if one exists. 
name= gets.chomp 
# Tell the user that they have a nice name. 
# The .capitalize() method returns the 
# capitalized version of the string. 
puts "#{name.capitalize}; nice name!" 
# Print a blank line. 
# Ask the user what their favorite store is. 
puts 'What is your favorite store? ' 
# Get user input; same as last time. 
store= gets.chomp 
# Make `store` upper-case. 
store= store.upcase 
# Mention the upper-case store name and ask the user for their favorite color. 
puts "So it's #{store}, huh. What about your favorite color? " 
# Get user input. 
color= gets.chomp 
# Make `color` lower-case. 
color= color.downcase 
# Mention the lower-case store name and ask for their favorite movie. 
puts "So you like #{color}, don't you? Well, then, what movie do you like most? " 
# Get user input. 
movie= gets.chomp 
# Swap the cases of all the letters. 
movie= movie.swapcase 
# Mention their favorite movie. 
puts "You like the movie #{movie} most? Okay, enough with that. " 
# Print a blank line. 
# Ask for a number. 
puts "Enter any number: " 
# Get user input. 
input1= gets 
# Print information about the number. 
puts "The number you entered: " 
puts "As Integer: #{input1.to_i.to_s}"      # Convert number to integer and then to a string. 
puts "As Float: #{input1.to_f.to_s}"        # Convert number to float and then to a string. 
# Ask for another number. 
puts "Enter another number: " 
# Get user input. 
input2= gets 
# Print information about the number. 
puts "The number you entered: " 
puts 'As Integer: ' + input2.to_i.to_s 
puts 'As Float: ' + input2.to_f.to_s 
# As you can see, you can also use the '+' sign to combine strings. 
# Print a blank line. 
# Print an exit message. 
puts "Exitting... Press return key to continue... " 
# Wait for return key press. 

The Output
Posted Image

(Fullsize Screenshot)

First Tutorial:
Hello World Introduction

Previous Tutorial:

Next Tutorial:
File I/O
  • 0

Also tagged with one or more of these keywords: ruby, variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download