Created
August 12, 2012 03:15
-
-
Save glucero/3329401 to your computer and use it in GitHub Desktop.
examples for rika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
def speak | |
puts "My name is #{@name}. I am a #{@occupation}." | |
end | |
def occupation(occupation) | |
@occupation = occupation | |
end | |
def initialize(name) | |
@name = name | |
end | |
end | |
class House | |
def residents | |
@people.each do |person| | |
person.speak | |
end | |
end | |
def move_in(person) | |
@people.push(person) | |
end | |
def initialize | |
@people = Array.new | |
end | |
end | |
# examples of how the above code works: | |
man = Person.new("Fred") | |
man.occupation("Garbage man") | |
home = House.new | |
home.move_in(man) | |
home.residents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal | |
def speak | |
if @type.eql?(:cat) | |
puts "meow!" | |
elsif @type.eql?(:dog) | |
puts "woof!" | |
end | |
end | |
def call(name) | |
self.speak if @name.eql?(name) | |
end | |
def initialize(type) | |
@type = type | |
end | |
end | |
class Dog < Animal | |
def initialize(name) | |
@name = name | |
super(:dog) | |
end | |
end | |
class Cat < Animal | |
def initialize(name) | |
@name = name | |
super(:cat) | |
end | |
end | |
# examples of how the above code works: | |
dog = Dog.new('Fido') | |
dog.call('Fido') | |
dog.call('Spot') | |
cat = Cat.new('Mittens') | |
cat.call('Mittens') | |
cat.call('Garfield') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = String.new # or name = "" | |
string.concat('Rika') # => Rika | |
string.upcase # => RIKA | |
string.downcase # => rika | |
string.reverse # => akiR | |
string.split('') # => ["R", "i", "k", "a"] | |
array = Array.new # or array = [] | |
array.push(1) # => [1] | |
array.push(2) # => [1, 2] | |
array.push(3) # => [1, 2, 3] | |
array.reverse # => [3, 2, 1] | |
array.first # => 1 | |
array.last # => 3 | |
array[0] # => 1 | |
array[1] # => 2 | |
array[2] # => 3 | |
hash = Hash.new # or hash = {} | |
hash['first'] = "Rika" # => { 'first' => 'Rika' } | |
hash['last'] = "Fama" # => { 'first' => 'Rika', 'last' => 'Fama' } | |
hash['first'] # => Rika | |
hash['last'] # => Fama | |
hash['first_name'].reverse # => akiR | |
array.push(name) # => [1, 2, 3, { 'first' => 'Rika', 'last' => 'Fama' } ] | |
array.last # => { 'first' => 'Rika', 'last' => 'Fama' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment