Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week4 homework #63

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?

Object is an Instance of class or variable of class.

2. What is a variable?

Variables are the memory locations which hold any data to be used by any program.
There are different types of variables supported by Ruby.

Global
Local
Class
Pseudo

3. What is the difference between an object and a class?

a class is a construct that defines a collection of properties and methods. Memory space is not allocated , when it is created.
an Object is an Instance of class or variable of class. Memory space is allocated, when it is created.

4. What is a String?

A String object in Ruby holds and manipulates an arbitrary sequence of one or more bytes, typically representing characters that represent human language.

5. What are three messages that I can send to a string object? Hint: think methods

substitution (gsub,replace), insertion ( insert) , Changing a section ([])

6. What are two ways of defining a String literal? Bonus: What is the difference between them?

SIngle quoted strings
Double quotes strings

Main difference is Double quotes support interpolation where as Single Quotes doesn't.
17 changes: 12 additions & 5 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@
@my_string = "Renée is a fun teacher. Ruby is a really cool programming language"
end

it "should be able to count the charaters"
it "should be able to count the charaters" do
result = @my_string.length
result.should be 66
end

it "should be able to split on the . charater" do
pending
result = #do something with @my_string here

result = @my_string.split('.')
#do something with @my_string here
result.should have(2).items
end

it "should be able to give the encoding of the string" do
pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
encodeing #do something with @my_string here
#pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
result = @my_string.encoding
result.should be Encoding.find("UTF-8")
#do something with @my_string here
#use helpful hint here
end
end
end


4 changes: 3 additions & 1 deletion week2/exercises/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ def initialize title = "Not Set", page_count = 0
@title = title
end

end
end

aBook = Book.new( "Test", 10)
11 changes: 11 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?

Indexing of array elements will be with Integers where Hash elements can be indexed with any type of elements, doesn't need to be integer . This makes hash indexing will not be in order, but by key value.

2. When would you use an Array over a Hash and vice versa?

Array might be preferred over Hash when order of the elements is known ( especially when elements are less in number).
Hash is preferred over Array when elements can be accesssed by key value.

3. What is a module? Enumerable is a built in Ruby module, what is it?

Module is a collection of methods and constants.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?

mixins in Ruby will be helpful in sharing code.

5. What is the difference between a Module and a Class?

Modules are about providing methods that you can use across multiple classes. We can think about them as "libraries" . Classes are about objects; modules are about functions.
70 changes: 0 additions & 70 deletions week3/homework/calculator_spec.rb

This file was deleted.

15 changes: 15 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ Please Read:

1. What is a symbol?

A Symbol is the most basic Ruby object you can create. It's just a name and an internal ID. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program.

2. What is the difference between a symbol and a string?

Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory.

3. What is a block and how do I call a block?

A block consists of chunks of code. You assign a name to a block.The code in the block is always enclosed within braces ({}).
A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block.
You invoke a block by using the yield statement.


4. How do I pass a block to a method? What is the method signature?

if the last argument of a method is preceded by &, then you can pass a block to this method and this block will be assigned to the last parameter.
In case both * and & are present in the argument list, & should come later.

5. Where would you use regular expressions?

while matching patterns in strings.

17 changes: 17 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@ The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?

## File.new("testfile", "r")
## File.open("testfile", "r")

2. How would you output "Hello World!" to a file called my_output.txt?

## File.open(my_output.txt, 'w') { |file| file.write("Hello World!") }
## File.write('my_output.txt', 'Hello World!')

3. What is the Directory class and what is it used for?

## Objects of class Dir are directory streams representing directories in the
## underlying file system. They provide a variety of ways to list directories
## and their contents.


4. What is an IO object?

## The IO class is the basis for all input and output in Ruby. IO is superclass of File.

5. What is rake and what is it used for? What is a rake task?

## Rake is make like program in Ruby.
## A Task is the basic unit of work in a Rakefile. Tasks have associated actions (possibly more than one) and a ## list of prerequisites. When invoked, a task will first ensure that all of its prerequisites have an
## opportunity to run and then it will execute its own actions.
10 changes: 10 additions & 0 deletions week4/homework/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
###

module Worker
def self.work
value = yield
value
end
end

## Still working on passing parameter.
6 changes: 4 additions & 2 deletions week4/homework/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
require "#{File.dirname(__FILE__)}/worker"

describe Worker do

it "executes a block and returns a string" do

it "executes a block and returns a string" do

result = Worker.work do
"hello"
end
result.should == "hello"

end

it "executes a block and returns a number" do
Expand Down
13 changes: 13 additions & 0 deletions week5/exercises/Rakefile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
desc "reads lines in names file"
task :rake1 do
f=File.open("names",'r+')
f.each do |line|
puts line
end
end

task :rake2 do
f= Dir.mkdir(class)
end