# Newbie ahead!: Ruby-Demo

**URL:** <https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467>\
**Category:** Ruby\
**Created:** [January 20, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467 "2006-01-20T17:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [January 20, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467/1 "2006-01-20T17:53:28Z")

</div>

Hi,

can someone point me to a ruby demo-script?

I want to show a friend the beauty of ruby and am myself fear if  
would try my best would acchieve the opposite of what I want.

It dont need to do anything sophisticated it only should show the  
Pros of programming in ruby…

Thanks a lot in advance for any help!  
Ruby!  
mcc

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [January 20, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467/2 "2006-01-20T18:28:53Z")

</div>

Meino Christian C. wrote:

> Hi,
> 
> can someone point me to a ruby demo-script?
> 
> I want to show a friend the beauty of ruby and am myself fear if  
> would try my best would acchieve the opposite of what I want.
> 
> It dont need to do anything sophisticated it only should show the  
> Pros of programming in ruby…
> 
> Thanks a lot in advance for any help!  
> Ruby!  
> mcc

You might want to try:

[http://tryruby.hobix.com/](http://tryruby.hobix.com/)

HTH,

Amr

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [January 20, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467/3 "2006-01-20T18:37:41Z")

</div>

Show them: blocks, OOP, “the Ruby way”.

(0…10).map{|x| x\*x}.select{|x| x \< 50}

Take a Java program and translate it to ruby step-by-step by stripping  
things 😉

* * *

class Person  
def initialize(newName)  
@name = newName  
end

def getName()  
return(@name);  
end

def setName(newName)  
@name = newName;  
end  
end

=\>

class Person  
def initialize(new\_name)  
@name = new\_name  
end

def name  
@name  
end

def name=(new\_name)  
@name = new\_name  
end  
end

=\>

class Person  
attr\_accessor :name  
end

=\>

Person = Struct.new(:name)

…

people = [Person.new(‘David’), Person.new(‘Amy’), Person.new(‘Paul’)]  
people.sort\_by{|p| p.name}

…

# Struct.new returns a class, so we can subclass it 🙂

class Person \< Struct.new(:name)  
include Comparable

def \<=\> other  
@name \<=\> other.name  
end  
end

people = [Person.new(‘David’), Person.new(‘Amy’), Person.new(‘Paul’)]

# A person has comparison methods now. \> == \< \<= \>= etc. can be used

now.

# The people Array has includes?, max, min etc.

# Everything just by adding include Comparable and def \<=\>.

if people[0] \< people[1] && people.includes? Person.new(‘David’)  
people.sort!  
end

* * *

All untested.

Jules

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [January 24, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467/4 "2006-01-24T16:39:26Z")

</div>

Here is a class that I just whipped up while I am also learning (just  
grabbed the pickaxe book about an hour ago). This code was inspired by  
the fib\_up\_tp(max) function on page 50. While I can’t claim it takes  
full advantage of Ruby’'s power it nonetheless demonstrates many  
facets of the language.

Imagine doing this in C! Even C++ this would be less “clean”.

If anyone has any suggestions on taking more advantage of Ruby in the  
code below, let me know! Of course, Fibs should inherit an array, and  
so fourth (hey, I’m only on page fifty :-p ) , but I believe this gives  
a great first look at the power of Ruby’s semantics to someone curious  
about Ruby.

class Fibs  
@foundFibs  
def initialize  
@foundFibs = Array.new(2, 1)  
end  
def lastIndex  
@foundFibs.size - 1  
end  
def nextFib  
@foundFibs.push(@foundFibs[-2] + @foundFibs[-1])  
self.largestFib  
end  
def largestFib  
@foundFibs.last  
end  
def up\_to\_num(max)  
self.nextFib while @foundFibs.last \< max  
@foundFibs.find\_all{|x| x \<= max}  
end  
def up\_to\_index(index)  
(self.lastIndex + 1).upto(index) {self.nextFib} if index \>  
self.lastIndex  
@foundFibs.first(index + 1)  
end  
def to\_s  
s = “[”  
@foundFibs.each {|name| s += name.to\_s + ", "}  
s[0…s.size-3] + “]”  
end  
def length  
@foundFibs.length  
end  
def   
self.up\_to\_index(index) if index \> self.lastIndex  
@foundFibs[index]  
end  
end

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [January 20, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/newbie-ahead-ruby-demo/63467/5 "2006-01-20T19:06:01Z")

</div>

> people = [Person.new(‘David’), Person.new(‘Amy’), Person.new(‘Paul’)]

people = %w{David Amy Paul}.map { |name| Person.new(name) }
