Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class names in Ruby always start with a capital letter.
- some source code demonstrating a class
- class Person
- attr_accessor :name, :age, :gender
- end
- attr stands for "attribute" and accessor roughly means "make these attributes accessible to be set and changed at will.
- person_instance = Person.new
- person_instance.name = "Robert"
- person_instance.age = 52
- person_instance.gender = "male"
- puts person_instance.name
- inheritance
- class Pet
- attr_accessor :name, :age, :gender, :color
- end
- class Cat < Pet
- end
- class Dog < Pet
- def bark
- puts "woof"
- end
- end
- class Snake < Pet
- attr_accessor :length
- end
- a_dog = Dog.new
- a_dog.bark
- puts a_dog.class #outputs Dog
- puts 2.class #outputs Fixnum numbers are objects of the Fixnum class
- puts "This is a test".length #outputs 14
- puts "foobar".sub('bar', 'foo') #outputs foofoo
- .sub only does one substitution at a time, on the first instance of the text to match, whereas gsub does
- multiple subsitutions at once.
- puts "this is a test".gsub('i','') #outputs ths s a test
- #force integers to float
- 9.to_f / 5
- #or
- 9.0 / 5
Advertisement
Add Comment
Please, Sign In to add comment