User:AJFarmar: Difference between revisions

(Created page with "{{mylangbegin}} {{mylang|Ruby|Very Active}} {{mylang|Java|Very Active}} {{mylang|Python|Active}} {{mylang|JavaScript|Active}} {{mylang|UNIX Shell|Rusty}} {{mylang|Visual Basic...")
 
 
(7 intermediate revisions by the same user not shown)
Line 5:
{{mylang|JavaScript|Active}}
{{mylang|UNIX Shell|Rusty}}
{{mylang|Visual Basic|Very Rusty}}
{{mylang|C++|Very Rusty}}
{{mylangend}}
==AJFarmar==
Anton J. Farmar, Computing student.
 
==Ruby==
===<code>String#word(Index)</code>:===
I made a little method that allowed me to replace <code>"String".split(" ")[index]</code> with <code>"String".word index</code>.
I know that this is relatively simple, but I still found it kinda neat.
 
<lang ruby>
class String
 
def word(index)
return self.split(" ")[index] if index.class != Range
return self.split(" ")[index].join(" ")
end
 
end
</lang>
 
And here's it in action. Note that I also implemented a renamed version; <code>String#w(index)</code>:
<lang ruby>
dummy = "Hello my deary, dearsome, deareary, dearlicious and dearful dearies!"
 
dummy.word 3 #=> "dearsome,"
dummy.w 3 #=> "dearsome,"
 
dummy.word 0..-1 #=>"Hello my deary, dearsome, deareary, dearlicious and dearful dearies!"
dummy.w 0..2 #=>"Hello my deary,"
</lang>
Anonymous user