Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
25 Cards in this Set
- Front
- Back
def initialize
|
class Language
def initialize(name, creator) @name = name @creator = creator end |
|
interpolation
|
#{@name}
|
|
create instance of a class. Must pass parameters
|
ruby = Language.new("Ruby", "Yukihiro Matsumoto")
|
|
Call a method
|
ruby.description
|
|
Scope
|
Variables and methods can be available everywhere (global), vars that are only available to certain methods (local variables), vars/methods that are members of a certain class, and ones that are only available to particular instances of a class (instance variables).
|
|
Scope notation
|
$ - global (if defined inside class, else no prefix needed), @-instance, @@-class
Can access global directly. Class need classname before '.' Instance needs object name before '.' |
|
Key value syntax for hashes
|
New: {hello: "Hello, world!"}
Old: {:hello=>"Hello, world!"} |
|
Initialise var
|
var =0
|
|
Return var
|
Just the name of the var, or return var
|
|
Accessing a key in a hash
|
puts my_hash["name"]
|
|
Creating a hash
|
Setting a variable equal to Hash.new creates a new, empty hash; it's the same as setting the variable equal to empty curly braces ({}).
|
|
.each iterator
|
friends.each { |x| puts "#{x}" }
|
|
Accessing value of a hash - 4 methods
|
stones.each { |k, v| puts "Key: #{k}, Value: #{v}" }
stones.each_key { |key| puts "#{key}" } stones.each_value { |val| puts "#{val}" } stones.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" } |
|
Class inheritance (of methods etc)
|
class MyApp < Application
end |
|
super method
|
When you call super from inside a method, that tells Within method, just write 'super'
Looks in the superclass of the current class and find a method with the same name as the one from which super is called. If it finds it, Ruby will use the superclass' version of the method |
|
attr_accessor syntax
|
attr_accessor :y, :x
After def initialize block in class block |
|
recursion
|
If the user didn't choose q/Q above, thus calling the return "str" (ie exit), then we just the method again from within itself. This is called "recursion" and is a very useful way to loop through a program.
|
|
Select method - what does it do?
|
It takes a block and passes in each item one after the other. If the block returns true, then that item is included in the output; if it returns false, it is left out.
|
|
PG gem connecting to database via hash, and closing connection
|
@@conn = PG.connect(:dbname =>'shelter', :host => 'localhost')
@@conn.close |
|
PG gem data insertion to database
|
sqlclient = "insert into clients (name, age) values ('#{name}', '#{age}')"
@@conn.exec(sqlclient) |
|
DSL
|
domain specific language, eg rspec
|
|
rspec DSL syntax
|
describe "::new" do #label for test, can use any word
it "creates a new Bank object" do #msge for test |
|
Creating instance variable within a Class
|
Has to be in def initialize.
|
|
When use rbenv rehash?
|
Every time you install a new version of Ruby or install a gem that provides a command (write 'gem name' in CLI and press enter to check), run rbenv rehash to make sure any new commands are shimmed. When you execute a shim, rbenv determines which Ruby version to use
|
|
Adding an instance to an array using a method
|
def add_client(client)
@clients << client end #if use = then not adding to end of array - just var |