Fun with yield
Author |
Message |
haskell
|
Posted: Wed Dec 27, 2006 10:39 pm Post subject: Fun with yield |
|
|
yield is possibly one of Ruby's most powerful features. It basically allows you to put a block of code into a function.
This could be a value, a string, a regex, function call, etc...
Consider:
Ruby: | def dynamic_square
return yield * yield
end
|
Ruby: | puts dynamic_square { 6 } |
Which results in 36.
Or:
Ruby: | def vowel_replace
line = yield
if line =~ /a|e|i|o|u/
line = line.gsub(/a|e|i|o|u/, 'Y')
end
result = line
end
|
Ruby: | puts vowel_replace {"The quick brown fox jumped over the lazy dog"} |
Which results in "ThY qYYck brYwn fYx jYmpYd YvYr thY lYzy dYg".
Ruby: | def lambda
return yield
end
|
Ruby: | lambda do
puts "Omg! Stealing from Lisp!!"
end
|
Ruby: | lambda do
puts "Lisp snatching again\n" * 10
end
|
Trivial examples. But they show a potential(particularly in AI).
So go off and experiment! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Wed Dec 27, 2006 11:53 pm Post subject: Re: Fun with yield |
|
|
haskell wrote: Consider:
Ruby: | def dynamic_square
return yield * yield
end
|
Ruby: | puts dynamic_square { 6 } |
Which results in 36. Remember in Ruby the last value evaluated will also be the result returned by default.
Consider:
Ruby: | def dynamic_square
yield * yield
end
|
Ruby: | > dynamic_square { 6 }
=> 36 |
![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu Dec 28, 2006 12:53 am Post subject: (No subject) |
|
|
code: | def vowel_replace
line = yield
if line =~ /a|e|i|o|u/
line = line.gsub(/a|e|i|o|u/, 'Y')
end
result = line
end |
How about...
code: | def vowel_replace
yield.gsub(/a|e|i|o|u/, 'Y')
end |
![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Thu Dec 28, 2006 12:57 am Post subject: (No subject) |
|
|
wtd wrote: How about...
code: | def vowel_replace
yield.gsub(/a|e|i|o|u/, 'Y')
end |
![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) Or... code: | irb(main):003:0> def vowel_replace (letter)
irb(main):004:1> yield.gsub(/a|e|i|o|u/, letter)
irb(main):005:1> end
=> nil
irb(main):007:0> vowel_replace('y'){"hello"}
=> "hylly" |
|
|
|
|
|
![](images/spacer.gif) |
haskell
|
Posted: Thu Dec 28, 2006 1:05 pm Post subject: More fun with yield |
|
|
Passing Values to Block Using yield
Ruby: | def pass_name
puts "What is your name?\n"
n = gets
yield(n)
end
|
Ruby: | pass_name do |name|
puts("Hello, \n " + name)
end
|
Which can also be dome as:
Ruby: | pass_name do |name|
puts "Hello, \n #{name}"
end
|
As you can see, the variable n of the pass_name method was passed to the block(do..end), and then used(under the new name, name, denoted by the | and | to be a variable of the block).
The block could also have been done with brackets({ and }) instead of do ..end, but it is multi-lined, so do..end is the "standard".
Have fun, and experiment . |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu Dec 28, 2006 1:11 pm Post subject: Re: More fun with yield |
|
|
haskell wrote: Passing Values to Block Using yield
Ruby: | def pass_name
puts "What is your name?\n"
n = gets
yield(n)
end
|
Or just:
code: | def pass_name
puts "What is your name?"
yield gets
end |
I also find it mildly odd that you're not aware that "puts" automatically adds a newline as necessary. In your code, the newline you use is extraneous. |
|
|
|
|
![](images/spacer.gif) |
haskell
|
Posted: Thu Dec 28, 2006 1:47 pm Post subject: (No subject) |
|
|
I am aware. I just prefer to be explicit with my intentions. It doesn't make any difference in the output, but I know exactly what it will do without thinking about the function call(old habits die hard).
I also use extra vars in the examples to show how vars can be passed using yield. Its not the passing of gets thats important in this example(thanks for the extra examples though). |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Fri Dec 29, 2006 9:14 am Post subject: (No subject) |
|
|
okay, but afterwards, show how that can be streamlined by getting rid of uneeded "stuff". Follow this trivial example:
Ruby: |
def is_odd?(number)
if number % 2 == 1 then
true
else
false
end
end
|
Can be shortened to:
Ruby: |
def is_odd?(number)
number % 2 == 1
end
|
Now, from a beginner's standpoint, the first makes more sense right? That's great, but they probably know that there has to be a better way of doing things, so then you show them, with the explanation of why it works exactly why it does. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|