Ruby Study Notes
Ruby usage
The first half of Ruby
We are defining, we are creating
For example, blue_crystal = 1, leaf_records = 5
The second half of Ruby
Putting things in motion
For example, pipe. catch_a_star
Conclusion: 1. Defining things 2. Putting those things into action Ruby is so simple to use
Supplement: Whenever you use a method, you will always be given something back, you can ignore it or use it
About nil, false, and true
Nil is without value, it is not zero. Zero is a number
For example, plastic_cup = nil means plastic_cup is empty (here plastic_cup is not undefined in Ruby)
False and true are similar to General languages. In Ruby, there is unless (true in nil and flase) Besides if judgment)
For example, print "Yes, plastic cup is up again! "If plastic_cup
Print "hardly, it is down." Unless plastic_cup
Other highlights
Double equals sign is a method
For example, approaching_guy. = (true)
Triple equals is a length of velvet rope, checking values much like the double equals, but it is a longer rope and It sags a bit in the middle
For example
If 1894 .. 1913 = year "Born" # Here we can see a range floating, not necessarily exact equal
End
Ruby class definition
Require 'endertromb'
Class wishmaker
Def initialize
@ Energy = rand (6)
End
Def grant (WISH)
If wish. length> 10 or wish. Include? ''
Raise argumenterror, "Bad wish"
End
End
End
Create todays_wishes = wishmaker. New
Everything in ruby is an object
This is very powerful.
For example
Number = 5 print number. Next output is 6
Print 5. The class outputs "integer", indicating that 5 is an integer object.
Print wishmaker. New. class will output "wishmaker"
Something about writing methods
1. Do not be surprised if people pass unexpected objects into your methods. An exception is reported for the input error value.
2. It is poor etiquette to change objects your method is given. Use DUP (x = x. DUP) for copy backup
3. The square brackets can be used to lookup parts inside any array, hash or string objects. Only [] methods are provided in these methods, and [] = methods are also provided.
4. Watch for runaway loops. Avoid while and.
Inheritance in ruby
Class toastybear <object; end indicates that toastybear inherits the object class.
In Ruby, all classes inherit the object class, and even the class is an object. It can be said that when you alter the object, you alter everything in ruby.
Differences between objects, classes, and modules in ruby
If object is the king, the one who has sired every other part of Ruby, then module is the poor waifish nun, shielding and protecting all her little Ruby townspeople children. (to complete the analogy: class is the village school teacher and kernel is the self-important
Colonel .)
The last time I inserted a movie, I can see how cool the author of this book is.