Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andriikot/ruby-lesson-5-classes
Ruby Lesson 5 classes
https://github.com/andriikot/ruby-lesson-5-classes
Last synced: about 2 months ago
JSON representation
Ruby Lesson 5 classes
- Host: GitHub
- URL: https://github.com/andriikot/ruby-lesson-5-classes
- Owner: AndriiKot
- Created: 2023-03-30T08:37:14.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-03-30T10:04:36.000Z (over 1 year ago)
- Last Synced: 2024-01-09T00:28:39.082Z (12 months ago)
- Language: Ruby
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ruby-lesson-5-classes
Ruby Lesson 5 classes```ruby
class Person
attr_accessor :name,:age
def initialize name,age
@name,@age = name,age
end
endclass Manager < Person
endmanager1 = Manager.new 'Mr. Joe',30
p manager1 # #
p manager1.class # Manager
p manager1.class.superclass # Person
p manager1.class.ancestors # [Manager,Person,Object,Kernel,BasicObject]
```
irb -r ./classes_for_ruby1.rb```cmd
Manager
Person
[Manager,Person,Object,Kernel,BasicObject]
```
____
____```ruby
class Animal
endp Animal.ancestors #[Animal,Object,Kernel,BasicObject]
module Kernel
def my_method1 = puts "Hello from Kernel!"
endclass Animal
def self.hello
puts "Hello my name is #{self.name}"
my_method1
end
endp Animal.hello
``````cmd
# Hello my name is Animal
# Hello from Kernel!
```
____
____```ruby
class Animal
endp Animal.class # Class
p Animal.class.superclass # Module
p Animal.class.ancestors # [Class,Module,Object,Kernel,BasicObject]module MyModule
end=begin
class Animal < MyModule # TypeError
end
=end
```___
___```node
class Person
NAME = 'Anonymous'
endname = Person
p name::NAME # 'Anonymous'
class Person
attr_accessor :name
def initialize(name)
@name = name
end
endoxi = name.new 'Oxi'
p oxi.name # "Oxi"
```
```cmd
"Anonymous"
"Oxi"
```
____
____```ruby
NAME = "Incognito"module Namespace
class Person
NAME = "Anonymus"
class << self
def hello_from_class
"Hello #{NAME}!"
end
def hello_from_main
"Hello #{::NAME}!"
end
end
end
endname = Namespace::Person
p name::NAME #"Anonymus"
p NAME # "Incognito"
p name.hello_from_class # "Hello Anodymus!"
p name.hello_from_main # "Hello Incogito!"
``````cmd
"Anonymus"
"Incognito"
"Hello Anodymus!"
"Hello Incogito!"
```