storing data getting data out data types ruby as a foundation program variables click icon to hear...
TRANSCRIPT
Storing data
Getting data out
Data types
Ruby as a foundation
Program Variables
Click icon to hear comments(the download may take a minute)
Assigning a value to a variable
The variable name on the left side of the equal sign gets a value assigned to it.
– A value can be assigned to give a name to a number
LegalAge = 21– A variable name may be easier to remember than a number and
thus, help to make programming easier– imagine having to remember and type 568.245 when you
want to reference the number of milliliters per pint
mlperpint = 568.245
Click icon to hear comments
Getting data out of variable
To use the value stored in a variable, use the name is if it were the value itself
– The value might be printed for display purposes
PRINT “The legal age is #{LegalAge}”– The value might be used for a calculation
legalBirthDate = DateToday - LegalAge– The value be used for a decision
IF age < LegalAge Then PRINT “too young”
Click icon to hear comments
Numbers or Letters?
Because of the way computers store and manipulate data, different types of variables are used.
Use a numeric data type for math operations Data type can affect
– the amount of memory used– the way decimals are handled– The number of digits that can be stored– how rounding or fractional values are handled
Click icon to hear comments
String Data Type
The letters, numbers, and symbols on the keyboard are called alphanumeric
string is often used as a shorter way of referring to alphanumerics.
Quotations marks surrounding text indicates string data.FirstName = “Petulla”
In VisualBasic, the words as string are used when the variable is defined.
Click icon to hear comments
Data types in Ruby and other languages
For simplicity, in Ruby a variable is considered a string type by default if input is from a keyboard
– String values need to be converted to numeric before performing math.
– Floating point data type can also be specified by assigning a value with a decimal point, otherwise integer is assumed
mlPerPint! = 568.245– In VisualBasic the words as single are used when the variable is
first defined.
Click icon to hear comments
Other Data Types in Ruby and symbol used
Values too large for Fixnum are automatically converted to Bignum—both are subclasses of integer
Integer– Whole numbers up to 1 less than the native machine
word are stored as Fixnum
Other number bases– Hexadecimal - precede value with 0x– Binary – precede value with 0b– Octal – precede value with 0
Click icon to hear comments
Other Data Types in VisualBasic and symbol used
In addition to the data types supported by Ruby, VisualBasic has several, such as:
– Boolean - True or False– Char – single Unicode character followed by lowercase c
“A”c
– Decimal – up to 29 digits followed by D 59.99D
– Date – days since year 1 enclosed in # #1/1/2001 1:00 PM#
– Double ± 10308 followed by R– Object– can store and convert to other types
Click icon to hear comments