# Using functions from lang/core.rb in a ruby class

**URL:** https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633
**Category:** Support, Help & Resources
**Created:** [October 30, 2018, 5:33pm UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633 "2018-10-30T17:33:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![j0le](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/j0le/32/769_2.png) [@j0le](https://in-thread.sonic-pi.net/u/j0le)
#### Post date: [October 30, 2018, 5:33pm UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633/1 "2018-10-30T17:33:59Z")

</div>

I want to write a class that has a function which uses in\_thread, like so:

```ruby
class MyClass
  def myFn
    in_thread do
      puts "test"
      play 60
    end
  end
end

x = MyClass.new
x.myFn

```

but it gives me this error:

> Runtime Error: [buffer 6, line 11] - NoMethodError  
> Thread death!  
> undefined method `in\_thread’ for #\<SonicPi::RuntimeMethods::MyClass:0x26cdf60\>

I know that in\_thread and other functions like play are defined in lang/core.rb.  
How can I access these functions from within a class?

Thanks in advance,  
Ole

PS:  
I’m a C# programer, but I’m completly new to ruby and sonic pi.  
Today I learned what a &block parameter is

---

<div class="post-metadata">

### Author: ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)
#### Post date: [October 30, 2018, 6:06pm UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633/2 "2018-10-30T18:06:02Z")

</div>

Hi Ole,

the short answer is that this isn’t supported. Sonic Pi isn’t a standard Ruby library, it’s a DSL built on top of Ruby. Whilst many aspects of Ruby are available to Sonic Pi, only the code described in the tutorials is officially supported.

---

<div class="post-metadata">

### Author: ![j0le](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/j0le/32/769_2.png) [@j0le](https://in-thread.sonic-pi.net/u/j0le)
#### Post date: [October 30, 2018, 6:18pm UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633/3 "2018-10-30T18:18:50Z")

</div>

Thanks Sam, for the quick answer 😊 .

For people that want to do this nevertheless:  
I found out, that you can do this:

```ruby
class MyClass
  def initialize(play, in_thread)
    @play = play
    @in_thread = in_thread
  end
  
  def myFn
    @play.call 70
    @in_thread.call do
      @play.call 60
    end
  end
end

x = MyClass.new(lambda {|*args| play *args}, lambda {|*args, &block| in_thread *args, &block})
x.myFn

```

However that is not elegant, because you must pass every function, you want to use, to the `new` function.

---

<div class="post-metadata">

### Author: ![maxl](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/maxl/32/726_2.png) [@maxl](https://in-thread.sonic-pi.net/u/maxl)
#### Post date: [October 31, 2018, 2:01am UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633/4 "2018-10-31T02:01:32Z")

</div>

Sam’s been pretty vocal about not wanting Sonic Pi to head into an object oriented direction. Still, for people who are really familiar with Ruby it feels weird to be locked into the procedural model of the Sonic Pi DSL.

Though it’s not supported, I do this all the time and I’ve developed a pretty elegant way of going about using my own classes as libraries.

You can really shoot yourself in the foot with this approach, especially with threads, so if you don’t know ruby in and out, avert your gaze.

This is how I do it:

```ruby
# Usually, put this defonce in a file like "/path/to/my/doorbell.rb"
# You must set override to true to make changes

defonce :use_doorbell, :override => false do
  class Doorbell
    def initialize(amp=1.0)
      @amp = amp
    end
    
    def self.set_runtime(rt)
      @@runtime = rt
    end
    
    def runtime
      @@runtime
    end
    
    def ring
      runtime.with_synth :pretty_bell do
        runtime.play 67, amp: @amp
        sleep 0.75
        runtime.play 60, amp: @amp
      end
    end
  end
  
  Doorbell.set_runtime(self)
end

# Usually load the library from the file
# run_file "/path/to/my/doorbell.rb"

use_doorbell

Doorbell.new(0.7).ring

```

---

<div class="post-metadata">

### Author: ![kniknoo](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/kniknoo/32/146_2.png) [@kniknoo](https://in-thread.sonic-pi.net/u/kniknoo)
#### Post date: [July 6, 2019, 7:33pm UTC](https://in-thread.sonic-pi.net/t/using-functions-from-lang-core-rb-in-a-ruby-class/1633/5 "2019-07-06T19:33:18Z")

</div>

Thank you for this thread. Y’all just saved me a LOT of effort. 🙂
