Hello fellow rubyists,
Please read the whole post despite the fact it's little long.
I have been reading the pick axe manual and am most inpressed with it.
I fully understand that their are some things that are not explained
till later but I don't think that is what I'm confused on. I have come
to a part in the manual which I don't understand and need some
assistance with the following:
Implementing a SongList Container
Class SongList
def deleteFirst
@songs.shift
end
def deleteLast
@songs.pop
end
end
Deffine “def”. Is “undef” it's oppisit? i.e. it causes an “def”ed thing
to cease to be “def”ed?
what does “deleteFirst” and “deleteLast” do?
“@songs” appears to be an array but what is .shift and .pop?
#Here is a more ruby type evaluation of the situation
.pop.is_a?(Popcorn)
if true add .butter and .salt
end
puts ha ha!
class SongList
def [](key)
if key.kind_of?(Integer)
@songs[key]
else
# …
end
end
end
What is “[]” for?
Why is the “def” thing in “()”?
What does the line “ @songs[key]” have to do with anything?
Why is “key” in “[]” instead of “()”
Why is “# ...” after an “else” statement; in mean “else” must have a
condition to work on right?
Blocks and Iterators
Class SongList
def [](key)
if key.kind_of?(Integer)
return @songs[key]
else
for I in 0...@songs.length
return @songs[i] if key == @songs[i].name
end
end
return nil
end
end
In the “I in 0...@songs.length” what are the “I” and “0” there for?
There's those “...” again what do they do?
Why is their nothing within these "[]" ?
Implementing Iterators
Def fibUpTo(max)
i1, i2 = 1, 1 # parallel assignment
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end
fibUpTo(1000) { |f| print f, " " }
what are does “yeild” do? It invoked what part of the code?
Concerning these characters “{ |f| print f, " " }”, why the “{}” why are
there “|” in the line? What are the “f”s for? What would or could go
in-between the (“ “)?
class Array
def find
for I in 0...size
value = self[i]
return value if yield(value)
end
return nil
end
end [1, 3, 5, 7, 9].find {|v| v*v > 30 }
what does the “for I in 0...size” statement do exactly? You have “for”
which I understand, but then you have an “I” which is not an object or
an integer or a string or a float. Then an “in” which does what?
Followed by a “0” then “...” then “size” and by then I'm totally
confused.
I like Ruby and I'm willing to read all the documentation that you
create but it will not help me if I don't get it.
1: help me to understand the documentation listed above.
···
--
Posted via http://www.ruby-forum.com/.