# Use methods from a library in my own class

**URL:** <https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521>\
**Category:** Ruby\
**Created:** [June 4, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521 "2009-06-04T20:27:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 4, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521/1 "2009-06-04T20:27:48Z")

</div>

I want to create a class that uses the RubyGnuplot library that can  
create a plot using one simple command and only add options if so  
desired. Below is what I have done so far and I was wondering if someone  
could provide some opinion on my approach. I ran into a problem where I  
allow the user to add 3 options but what I really want is access to all  
the library’s methods so colors and lineweights, etc can be changed if  
so desired. Is there a better approach than using a hash input like I’m  
doing? Thank you!

#For simple 2D line plots. File is called ‘my\_plotter.rb’

require ‘gnuplot’  
class Data2d  
def self.plot(x, y, options={})  
Gnuplot.open do |gp|  
Gnuplot::Plot.new( gp ) do |plot|

plot.title “#{options[:title]}”  
plot.ylabel “#{options[:y\_label]}”  
plot.xlabel “#{options[:x\_label]}”

plot.data \<\< Gnuplot::DataSet.new( [x, y] ) do |ds|  
ds.with = “lines”  
end  
end  
end  
end  
end

#Now all users need to do is create the data,

require ‘my\_plotter’

x = [1,2,3,4,5]  
y = [5,6,3,8,9]

#then run the command,

Data2d.plot(x,y)

#or, with options,

Data2d.plot(x,y,:x\_label=\>“Time”,:y\_label=\>“Values”, :title=\>“My First  
Plot”)

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 4, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521/2 "2009-06-04T20:40:20Z")

</div>

Jason L. wrote:

> require ‘gnuplot’  
> class Data2d  
> def self.plot(x, y, options={})  
> Gnuplot.open do |gp|  
> Gnuplot::Plot.new( gp ) do |plot|
> 
> plot.title “#{options[:title]}”  
> plot.ylabel “#{options[:y\_label]}”  
> plot.xlabel “#{options[:x\_label]}”

Untested…

```
 options.each {|opt,val| plot.send opt, val}
```

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 4, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521/3 "2009-06-04T21:44:49Z")

</div>

> ```
> options.each {|opt,val| plot.send opt, val}
> 
> ```

Thank you. That is a great idea and it works!

2 questions:

1. I see that your send method on plot.send is a method from  
Gnuplot::Plot.new but what exactly is it doing? My guess is it takes  
‘val’ and sends it to the plot method that corresponds with ‘opt’ as it  
iterates through my hash.

2. How do I apply your idea to methods within an embedded array block  
such as “plot.data” from my code:

plot.data \<\< Gnuplot::DataSet.new( [x, y] ) do |ds|  
ds.with = “lines”  
end

which has methods such as ds.linewidth, ds.title, etc? I tried multiple  
things like creating 2 hashes but can’t get my hash values to pass into  
the embedded array. Thank you!

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 4, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521/4 "2009-06-04T21:55:05Z")

</div>

Jason L. wrote:

> 1. I see that your send method on plot.send is a method from  
> Gnuplot::Plot.new but what exactly is it doing? My guess is it takes  
> ‘val’ and sends it to the plot method that corresponds with ‘opt’ as it  
> iterates through my hash.

The #send method is common to all ruby objects:

------------------------------------------------------------ Object#send  
obj.send(symbol [, args…]) =\> obj  
obj. **send** (symbol [, args…]) =\> obj

```
  From Ruby 1.8

```

* * *

```
  Invokes the method identified by symbol, passing it any arguments
  specified. You can use __send__ if the name send clashes with an
  existing method in obj.

     class Klass
       def hello(*args)
         "Hello " + args.join(' ')
       end
     end
     k = Klass.new
     k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
```

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [June 4, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/use-methods-from-a-library-in-my-own-class/169521/5 "2009-06-04T21:57:27Z")

</div>

Jason L. wrote:

> 1. How do I apply your idea to methods within an embedded array block  
> such as “plot.data” from my code:
> 
> plot.data \<\< Gnuplot::DataSet.new( [x, y] ) do |ds|  
> ds.with = “lines”  
> end
> 
> which has methods such as ds.linewidth, ds.title, etc? I tried multiple  
> things like creating 2 hashes but can’t get my hash values to pass into  
> the embedded array. Thank you!

Something like this?

require ‘gnuplot’  
class Data2d  
def self.plot(x, y, options={})  
Gnuplot.open do |gp|  
Gnuplot::Plot.new( gp ) do |plot|  
options.each {|opt,val| plot.send opt, val}  
ds\_options = options[:ds]  
plot.data \<\< Gnuplot::DataSet.new( [x, y] ) do |ds|  
ds\_options.each {|opt,val| ds.send opt, val}  
end  
end  
end  
end  
end

Data2d.plot(x,y,:x\_label=\>“Time”,:y\_label=\>“Values”, :title=\>“My First  
Plot”, :ds =\> {:with =\> “lines”})
