Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
class Dog
def set_name( aName )
@myname = aName
end
def get_name
return @myname
end
def gav
return 'r-r-r-r!'
end
end
dog1 = Dog.new
puts(dog1.get_name)
puts(dog1.gav)
class A
def initialize(a=10)
@a = a
end
end
a = A.new # @a = 10
b = A.new(20) # @a = 20def initialize(a=nil) @a = a || 10 end
class Test
def initialize *args
if args.size == 1
puts "first constructor"
elsif args.size == 2
puts "second constructor"
else
raise "Required 1 or 2 parameters"
end
end
end
Test.new("asdf") #=> first constructor"
Test.new("asdf","asdf") #=> "second constructor"
Test.new() #=> `initialize': Required 1 or 2 parameters (RuntimeError)
class SimpleParser
{
public SimpleParser()
{
}
public SimpleParser(String fileName)
{
}
public SimpleParser(Stream stream)
{
}
public SimpleParser(BinaryReader reader)
{
}
}
* This source code was highlighted with Source Code Highlighter.
Table 2.1. Example variable and class names
----------------------------------------------------------+---------------
Variables | Constants and
| Class Names
----------------------------------------------------------+---------------
Local | Global | Instance | Class |
----------------+--------------+--------------+-----------|
name | $debug | @name | @@total | PI
fish_and_chips | $CUSTOMER | @point_1 | @@symtab | FeetPerMile
x_axis | $_ | @X | @@N | String
thx1138 | $plan9 | @_ | @@x_pos | MyClass
_26 | $Global | @plan9 | @@SINGLE | JazzSong
--------------------------------------------------------------------------
Покорим Ruby вместе! Капля четвертая