ruby Ruby on rails类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby on rails类相关的知识,希望对你有一定的参考价值。


#Classes

Everything in Ruby is a class. A class is sort of a template we use to represent 
things in our code. We use these templates to build instances of an object. 
Think of a class as schematic that we can use to build objects.

class Car
  def honk
    puts "Beeeeeeeeep!"
  end
end

We now have a class called Car. We can use it to create instances or objects 
of that class using the new function. Those objects will have a function 
called honk that we can call. Functions attached to objects are also called 
instance methods or just methods for short. For example:

my_car = Car.new
my_car.honk


There is also a method called class we can invoke to see the class of an instance. 
look at the class of a Car objects as well as some other value types 

class Car
  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new

puts my_car.class
puts "cosa".class
puts 4.class


#State in classes

Aside from being a container for functions, classes and instances can also save 
values specific to an instance. We call this state in programming. 
Keeping with our car example, think of characteristics that can change from car to car. 
I can think of color, make, model and year. We want to have a way to represent that in our class.

#Instance variables
A class can have instance variables. An instance variable is a variable that contains 
a value specific to each instance of the class. These variables are also accessible 
by the methods of that instance. We mark a variable as an instance variable preceding 
its name with an at @ symbol. For example:

class Car
  def initialize(color)
    @color = color
  end

  def honk
    puts "Beeeeeeeeep!"
  end

  def print_color
    puts @color
  end
end

my_car = Car.new "red"
other_car = Car.new "grey"

my_car.print_color
other_car.print_color


#Initialization

Wait a minute... whats this initialize method? That is a method called automatically 
when you create an instance with the new method. This is called a constructor, 
a function that is called when instances a created (or constructed if you will). 
This is the way we set values for instance variables, by sending them as values for new. 
The values are in turn sent to initialize where we can set our instance variables.



--------


#Class variables

We can also create class variables that are shared by all instances of that class. 
For example we can count the amount of cars we have created so far:

class Car
  @@total = 0

  def initialize(color)
    @color = color
    @@total = @@total + 1
  end

  def honk
    puts "Beeeeeeeeep!"
  end

  def print_color
    puts @color
  end

  def print_total
    puts "So far we've got #{@@total} cars."
  end
end

my_car = Car.new "red"
other_car = Car.new "rainbow"

other_car.print_total

------

#Accessor methods

By default we cant directly access the instance variables of a given instance. 
Only methods have access to these values. We can, however, use attr_accessor 
to provide access to them. With direct access to the color variable we can do 
away with the print_color method:


#add gray to my_car.color that had yellow color


class Car
  attr_accessor :color

  def initialize(color)
    @color = color
  end

  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new "yellow"
other_car = Car.new "brown"

my_car.color = "grey"

puts my_car.color
puts other_car.color


-----------

#Inheritance

Ruby also allows us to extend a class by using inheritance. 
This is useful if we have a generic class that we want to use as a base to 
create more specific classes:

class Animal
  def initialize(name)
    @name = name
  end

  def describe
    puts "This animal's name is #{@name}"
  end
end

class Dog < Animal
end

class Cat < Animal
end

class Human < Animal
  def initialize(name, salary)
    super(name)
    @salary = salary
  end

  def describe_with_salary
    puts "This human's name is #{@name} and its salary is #{@salary}"
  end
end

dog = Dog.new "Winston Furchill"
cat = Cat.new "David Meowie"
human = Human.new "Johnny Appleseed", 12000

dog.describe
cat.describe
human.describe
human.describe_with_salary




#Class methods and instance methods

Just like we can create class variables, we can also create class methods. 
We can create class methods using the self.methodname syntax:

class Car
  attr_accessor :color

  @@total = 0

  def self.total
    return @@total
  end

  def initialize(color)
    @color = color
    @@total = @@total + 1
  end

  def honk
    puts "Beeeeeeeeep!"
  end
end

my_car = Car.new "red"
other_car = Car.new "rainbow"

puts "So far we've got #{Car.total} cars."

以上是关于ruby Ruby on rails类的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Ruby on Rails 中向 select_tag 添加一个类

ruby on rails on windows

Ruby on rails - 静态方法

Ruby On Rails:用于其他语言的复数形式

思考Ruby On Rails的底层代码(Ruby on Rails 開發秘籍 | Ruby on Rails 快速入門)

Ruby on Rails学习二在线学习资料的整理