Begging Ruby Chapter 1 Flashcards
What is object orientation
everything is an object guitar amps studio
Data Types
variable can hold these
Control Structures
variable can hold these
Is Ruby Open Source?
Yes
Ruby Interpreter
This is a ruby program that understands other programs written in the Ruby language, along with a collection of extensions and libraries to make your Ruby more fully featured
irb: Interactive Ruby
irb stands for “Interactive Ruby.” “Interactive” means that as soon as you type something, your computer will immediately attempt to process it. Sometimes this sort of environment is called an immediate or interactive environment.
object-oriented programming language
In the simplest sense, this means that your Ruby programs can define and operate upon concepts in a real-world fashion. Your program can contain concepts such as “people,” “boxes,” “tickets,” “maps,” or any other concept you want to work with. Object-oriented languages make it easy to implement these concepts in
a way that you can create objects based upon them. Object oriented languages can then act upon and understand the relationships between these concepts in any way you define.
nouns / verbs
Objects are nouns, methods are verbs.
3 corners of OOP
Encapsulation, Inheritance, Polymorphism
OOP
Style of programming where you create representations of “types” of objects like guitars and amps (Classes), and then can create specific objects like 69 strat, black les paul, 80s peavey, (instance of an object) that intend to use with each other.
attr_accessor
attr stands for “attribute,” and accessor roughly means “make these attributes accessible to be set and changed at will.”
print vs puts
puts automatically moves the output cursor to the next line
what can a ruby variable contain
numbers, text, and other data structures
class
A class is the definition of a single type of object
standard attribute syntax
attr_accessor :name, :age, :gender
what does inheritance allow you to do
Inheritance allows different classes to relate to one another and group concepts by their similarities
classless method
def guitar_strum
puts “brrriiing”
end
guitar_strum
Class
Class: A class is a definition of a concept in an object-oriented language such as Ruby. We created classes called Pet, Dog, Cat, Snake, and Person. Classes can inherit features from other classes, but still have unique features of their own.
Method
Method: A method represents a set of code (containing multiple commands and statements) within a class and/or an object. For example, our Dog class objects had a bark method that printed “Woof!” to the screen. Methods can also be directly linked to classes, as with fred = Person.new, where new is a method that creates a new object based upon the Person class. Methods can also accept data—known as arguments or parameters—included in parentheses after the method name, as with puts(“Test”).
Arguments/parameters
Arguments/parameters: These are the data passed to methods in parentheses (or, as
in some cases, following the method name without parentheses, as in puts “Test”). Technically, you pass arguments to methods, and methods receive parameters, but for pragmatic purposes, the terms are interchangeable.
Kernel
Some methods don’t require a class name to be usable, such as puts. These are usually built-in, common methods that don’t have an obvious connection to any classes. Many of these methods are included in Ruby’s Kernel module, a module that provides functions that work from anywhere within Ruby code without being explicitly referred to.
Experimentation
One of the most fulfilling things about programming is that you can turn your dreams into reality. The amount of skill you need varies with your dreams, but generally if you want to develop a certain type of application or service, you can give it a try. Most software comes from necessity or a dream, so keeping your eyes and ears open for things you might want to develop is important. It’s even more important when you first get practical knowledge of a new language, as you are while reading this book. If an idea crosses your mind, break it down into the smallest components that you can represent as Ruby classes and see if you can put together the building blocks with the Ruby you’ve learned so far. Your programming skills can only improve with practice.
simple do if syntax
age = 10
puts “You’re too young to use this system” if age
string literal
When a string is embedded directly into code, using quotation marks as earlier, the con- struction is called a string literal. This differs from a string whose data comes from a remote source, such as a user typing in text, a file, or the Internet. Any text that’s pre-embedded within a program is a string literal.
interpolation
interpolation refers to the process of inserting the result of an expression into a string literal “#{x+y}”
“Test” + “Test”
TestTest
“Test”.capitalize
Test
“Test”.downcase
test
“Test”.chop
Tes
“Test”.next
Tesu
“Test”.reverse
tseT
“Test”.sum
416
“Test”.swapcase
tEST
“Test”.upcase
TEST
“Test”.upcase.reverse
TSET
“Test”.upcase.reverse.next
TSEU
regular expression (string)
A regular expression, therefore, is a string that describes a pattern for matching elements in other strings.
basic substitution
puts “yo la tango”.sub(‘tango’, ‘mango’)
regular expression
puts “foobar”.sub(‘bar’, ‘foo’)
regular expression substitution of first two characters syntax
x.sub(/^../, ‘Hello’)
iterate through a string and have access to each section of it separately
“xyz”.scan (/./) { |character| puts character }
any alphanumeric character or an underscore
\w
regular expression argument general syntax
(/something/)
Anchor for the beginning of a line
Anchor for the end of a line
$
Anchor for the start of a string
\A
Anchor for the end of a string
\Z
Any character
.
Any letter, digit, or underscore
\w