Ruby/Language Basics/Variables

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Array elements automatically assigned to variables

x,y,z = [1,2,3] # Array elements automatically assigned to variables



Assign constant array value to three variables

a, b, *c = [12, 14, 178, 89, 90]
puts a                                 # => 12
puts b                                 # => 14
puts c                                 # => [178, 89, 90]



Assign values to more than one variable in one statement

array = [:red, :green, :blue]
c, a, b = array
puts a                                 # => :green
puts b                                 # => :blue
puts c                                 # => :red



create a variable and compare it with a number.

# If the variable and number are equal, the code is executed.
x = 256
if x == 256
  puts "x equals 256"
end
# => x equals 256



Define a variable to hold an integer

x = 10
puts x



Doing a calculation

 
temperature = 36
puts "The temperature is " + String(temperature) + "."
temperature = temperature + 5
puts "Now the temperature is " + String(temperature) + "."



Everything Is an Object

class MyClass
  def my_method
  end
end
a_class = MyClass
an_object = MyClass.new
a_method = an_object.my_method
print 42.class, " "
print a_class.class, " "
print an_object.class, " "
print a_method.class, " "
print a_method.methods.class



Exchange three value with using new variables

a, b, c = :red, :green, :blue
c, a, b = a, b, c
puts a                                 # => :green
puts b                                 # => :blue
puts c                                 # => :red



Exchange value with an extra variable

a, b = 1, 2
x = a
a = b
b = x



Exchanging Values Without Using Temporary Variables

a = 1
b = 2
a, b = b, a
puts a                                 # => 2
puts b                                 # => 1



give a value a name by assigning it to a variable

hi = "Hello, Matz!"
puts hi # => Hello, Matz!



Interpolation with two variables

n, animal = 2, "mice"
"#{n+1} blind #{animal}"  # => "3 blind mice"



Math calculation

1 + 2                    # => 3: addition
1 * 2                    # => 2: multiplication
1 + 2 == 3               # => true: == tests equality
2 ** 1024                # 2 to the power 1024: Ruby has arbitrary size ints



Objects Might Have Their Own Methods

class MyClass
  def speak
    puts "Ruby!"
  end
end
first_object = MyClass.new
second_object = MyClass.new
def first_object.sing
  puts "Give My Regards to Broadway"
end
first_object.speak
second_object.speak
first_object.sing
second_object.sing



Prefix and variable type

$files          # A global variable
@data           # An instance variable
@@counter       # A class variable
empty?          # A Boolean-valued method or predicate
sort!           # An in-place alternative to the regular sort method
timeout=        # A method invoked by assignment



Storing Data in Variables

# A standard variable starts with a lowercase letter, a to z, 
# or an underscore, _, followed by any number of name characters. 
# A name character is a lowercase letter, an uppercase letter, a digit, or 
# an underscore. 
 
temperature = 34
puts temperature



Swap the value of two variables

a, B = 1, 2     # Same as x = 1; y = 2
a, b = b, a     # Swap the value of two variables



understand the difference between numbers and digits.

# 12 is a number, but "12" is a string of two digits.
puts  12  +  12
puts "12" + "12"
puts "12  +  12"
# 24
# 1212
# 12  +  12



Variables allow you to write and use programs that work upon varying data.

x = 100
y = 10
puts x - y



variables can also be assigned the results of an expression

x = 50
y = x * 100
x += y
puts x



variables can point to any kind of object

var = "just another " + "string"
puts var
var = 5 * (1+2)
puts var



Variables in a Class

# The global variables are always preceded by the dollar sign ($).
# using the class variable @@no_of_customers
class Customer
@@no_of_customers=0
end



==x, Same as/td>




x, y = 1, 2     # Same as x = 1; y = 2