-
Notifications
You must be signed in to change notification settings - Fork 1
02 variables
Steve53 edited this page Sep 1, 2015
·
2 revisions
View the source code.
File
ruby variables.rb
Interpreter
$irb
irb(main):001:0> a = 3
=> 3
irb(main):002:0> m = "Steve"
=> "Steve"
irb(main):003:0> n = 'Perry'
=> "Perry"
irb(main):004:0> puts "#{m} Edward #{n} is #{a} years old."
Steve Edward Perry is 3 years old.
=> nil
Demonstrates declaring and initializing variables of several types. Demonstrates performing operations on variables and printing the values of variables.
In Ruby, a variable is an object. That is, a variable is an instance of a class:
irb(main):016:0* a.class
=> Fixnum
irb(main):017:0> m.class
=> String
A numeric constant is an object.
irb(main):022:0* 17.class
=> Fixnum
irb(main):023:0> 17.modulo 5
=> 2
To see the methods we can call on the numberic variable a, use a.class.instance_methods:
irb(main):024:0> a.class.instance_methods
=> [:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod,
:fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, ...
Try calling some methods on the variable a:
irb(main):037:0* a.> 4
=> false
irb(main):038:0> a.size
=> 8
irb(main):039:0> a.even?
=> false
irb(main):040:0> a.integer?
=> true