Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Ruby (learning and language comparisons)

For general discussion related FlowStone

Re: Ruby (learning and language comparisons)

Postby tulamide » Sat Mar 07, 2020 5:02 pm

You don't like OOP. I get it. But that doesn't mean everybody else has to dislike it. Again, my example proves nothing, as it wasn't intended to show what's the best way to deal with it, but to help people who don't know the concept, understanding it better by having a real life example of animals, to which most can relate to.

And telling people, who want to be able to use a script language that's completely grounded and optimized for OOP, to forget about OOP and do it the old way, is not helping, but making it more difficult for those. That's like telling a car owner to use the car like a carriage. Arrays are objects, strings are objects, arrays store objects only. Now tell them to create one array for strings and one for numbers to store their related data (as you would ignoring oop), instead of putting everything related in one array. Tell them not to use classes to build something like a spline envelope generator, tell them to ignore oop and instead store the 500 shapes, you use in your synth, in some manually allocated memory block (good luck with that in Ruby). Etc.

If you want a streamlined, fast program, forget about Flowstone and program your synth in Assembler alone. When you use Flowstone though, and want to make use of Ruby in Flowstone as well, you have to understand the concepts of OOP - wether you like it or not.

DSPRobotics themselves didn't understand the core mechanics of Ruby. Result: The stock modules contain C-like code in RubyEdits, that's highly inefficient, slow and error-prone.

What you did achieve though, is that I won't do another try to help people trying to dive into Ruby. It's not worth the punishment I get for having dared to.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby (learning and language comparisons)

Postby Spogg » Sat Mar 07, 2020 6:01 pm

Well I for one really appreciated you doing this tulamide.
It was a useful revision lesson for me and I thank you deeply for spending your precious time on it. :D

As you said so well, if you want to make use of Ruby in Flowstone you need to have a decent grip on OOP; what it’s about and how it functions etc.

Ruby is a great asset in FS. I mean, for example, how else could you sort a large list of numbers in green?

Thanks again!

Spogg
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England

Re: Ruby (learning and language comparisons)

Postby MichaelBenjamin » Sat Mar 07, 2020 6:26 pm

.
Last edited by MichaelBenjamin on Mon Sep 21, 2020 10:34 am, edited 1 time in total.
MichaelBenjamin
 
Posts: 275
Joined: Tue Jul 13, 2010 1:32 pm

Re: Ruby (learning and language comparisons)

Postby trogluddite » Sat Mar 07, 2020 6:42 pm

OOP vs. Imperative? In a wider sense, there are certainly valid arguments on both sides - no single programming paradigm is going to be best fit for everything (we don't use OOP for our sample-rate audio code, for example).

Pragmatically? Ruby is what FS actually has in practice, so OOP vs. others is a moot question - I get on with using what's actually there, and examples like tulamide's are valuable because they're the kind of thing that FS users might need in practice.

MichaelBenjamin wrote:So all the ramblings aside, i guess the example with the zoo animals like i wrote it, can aswell be similarily written in ruby?

Yes, you can write imperative code in Ruby in practice. There's nothing to stop you from writing methods in the "top-level" namespace which make no use of "self", so that they're effectively just functions. You still have the overheads of the built-in data types being objects, and there's no strict type checking, but that doesn't really change the semantics of the code.
Code: Select all
Animal = Struct.new(:name, :class, :order, :family, :species)
Animals = []

def add_animal(animal)
  fail(TypeError, "Animal expected") unless animal.kind_of?(Animal)
  Animals << animal
end

def is_carnivore?(animal)
  fail(TypeError, "Animal expected") unless animal.kind_of?(Animal)
  animal.class == "carnivore"
end


If you go bonkers with code-blocks and anonymous functions, you can even emulate much of functional languages like Lisp and Haskell - though it would be a pretty painful way of doing it!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby (learning and language comparisons)

Postby MichaelBenjamin » Sat Mar 07, 2020 6:49 pm

.
Last edited by MichaelBenjamin on Mon Sep 21, 2020 10:34 am, edited 1 time in total.
MichaelBenjamin
 
Posts: 275
Joined: Tue Jul 13, 2010 1:32 pm

Re: Ruby (learning and language comparisons)

Postby tulamide » Sat Mar 07, 2020 7:12 pm

MichaelBenjamin wrote:could you aswell write it this way:

Code: Select all
Animal = Struct.new(:name, :class, :order, :family, :species)
Animals = []

def add_animal(animal)
  animal.kind_of?(Animal)
    Animals << animal
end

def is_carnivore?(animal)
  animal.class.kind_of?("carnivore")
end

No. The code didn't make the neccessary object declarations for accessing object oriented methods. You can do it easily by extending my example though, as it contains those declarations.

MichaelBenjamin wrote:i dont understand the ?, in the add_animal it seems to be used like an if(condition), in the is_carnivore it seems to return a boolean?
The '?' at the end of the method name is a Ruby style convention, so that everyone reading the code immediately recognizes that this method only returns a boolean. You could use it directly, for example
Code: Select all
my_string = "George is eating meat" if is_carnivore?(animal)


EDIT: Of course, animal in the code line above is just a placeholder for the real struct you pass. I thought I should point that out!
Last edited by tulamide on Sat Mar 07, 2020 7:18 pm, edited 2 times in total.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby (learning and language comparisons)

Postby MichaelBenjamin » Sat Mar 07, 2020 7:12 pm

.
Last edited by MichaelBenjamin on Mon Sep 21, 2020 10:34 am, edited 1 time in total.
MichaelBenjamin
 
Posts: 275
Joined: Tue Jul 13, 2010 1:32 pm

Re: Ruby (learning and language comparisons)

Postby trogluddite » Sat Mar 07, 2020 7:39 pm

The lack of "return" is quite common in Ruby. By default, the result of the last expression before the method exits is always returned - I could have explicitly stated return animal.class == "carnivore", and it would have done the same thing.

And reading my own code again, there's probably something else worth clarifying...

In Ruby, names beginning with a capital letter are taken as being constants (class names being just a constant that has been assigned a Class definition as its "value"). So "Animal" is a constant, and is available within the methods as a reference to the Struct definition. If I had used lower-case "animal" for that, it would be taken as just a local variable, and it wouldn't have been visible inside the methods.

Strictly speaking, methods can begin with a capital letter, but it often confuses Ruby's name look-up, so it's always best to stick with lower case for method names. The convention is to always use little_snake_case, though many FS-specific methods don't observe this as their names are copied from the Windows API functions that they wrap.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby (learning and language comparisons)

Postby MichaelBenjamin » Sat Mar 07, 2020 7:41 pm

.
Last edited by MichaelBenjamin on Mon Sep 21, 2020 10:34 am, edited 3 times in total.
MichaelBenjamin
 
Posts: 275
Joined: Tue Jul 13, 2010 1:32 pm

Re: Ruby (learning and language comparisons)

Postby MichaelBenjamin » Sat Mar 07, 2020 7:56 pm

.
Last edited by MichaelBenjamin on Mon Sep 21, 2020 10:34 am, edited 1 time in total.
MichaelBenjamin
 
Posts: 275
Joined: Tue Jul 13, 2010 1:32 pm

PreviousNext

Return to General

Who is online

Users browsing this forum: Google [Bot] and 81 guests