# Re: Need help about Ruby with text file

**URL:** <https://www.ruby-forum.com/t/re-need-help-about-ruby-with-text-file/68831>\
**Category:** Ruby\
**Created:** [September 15, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/re-need-help-about-ruby-with-text-file/68831 "2006-09-15T19:53:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 15, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/re-need-help-about-ruby-with-text-file/68831/1 "2006-09-15T19:53:07Z")

</div>

> By the way, may I know that is there any function like e.g.  
> left(line,  
> 3), right(line, 2) and mid(line, 1, 4) to extract letter from a line,  
> please?  
> e.g.  
> mid(gghhii,3,2) return hh  
> left(gghhii,3) return ggh  
> right(gghhii,3) return hii

Remember that arrays and strings start at index 0, not index 1.

str = “gghhii”  
p str[0, 3] #=\> “ggh”  
p str[-3, 3] #=\> “hii”  
p str[2, 2] #=\> “hh”

class String  
def left( chars )  
self[0, chars]  
end  
def right( chars )  
self[-chars, chars]  
end  
def mid( start, chars )  
self[start, chars]  
end  
end

p str.left( 3 ) #=\> “ggh”  
p str.right( 3 ) #=\> “hii”  
p str.mid( 2, 2 ) #=\> “hh”

For more ways to manipulate strings, please type “ri String” into your  
prompt.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 16, 2006, 8:13am UTC](https://www.ruby-forum.com/t/re-need-help-about-ruby-with-text-file/68831/2 "2006-09-16T08:13:01Z")

</div>

Hai, Gavin K.

Thanks for your reply!  
Somebody talk about “Class” in Ruby to me.  
May I know that is “Class” a way to make my own fucntion only, please?  
Am I right? If not, what else “Class” can do?  
Your reply give me a new thought to do script with my way!

Thanks  
Regards  
Oscar

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 16, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/re-need-help-about-ruby-with-text-file/68831/3 "2006-09-16T12:46:17Z")

</div>

Hi Oscar,

On Sat, 16 Sep 2006 15:13:56 +0900, Oscar L. wrote:

> Oscar  
> ‘Class’ is a big topic!  
> Maybe start here: [http://pine.fm/LearnToProgram/?Chapter=09](http://pine.fm/LearnToProgram/?Chapter=09)  
> (Watch out for the Dragon!!!)
