simple DSL in Ruby - ruby

I would like to make a DSL in Ruby, where I will can using simple commands do something. For example "GO PRINT 10 *" will print 10 stars
or "GO PRINT 5 &" will print 5 ampersand. Anyone know good tutorials ?
I heard about the gem docile It is worthy of attention?

Here are some links relevant to what you want to do:
https://robots.thoughtbot.com/writing-a-domain-specific-language-in-ruby
Tutorials for writing DSL in Ruby
https://www.leighhalliday.com/creating-ruby-dsl
If you want a very flexible DSL (i.e., one in which you can't tell that it's actually Ruby, which sounds like what you want), I'd suggest learning a language such as Racket which is very well-geared towards creating all sorts of languages, e.g., a brainf*ck clone, a stack-based calculator, these, and these.

Related

How would someone make their own programming language

So i'm trying to make my own Programming Language but don't really know how. I have looked it up but people say try using Python to make it, well i mean how do you make your own language like to use python you would have to Install it, Well to use my language you would have to install it aswell. How would someone make it also add Syntax Highlighting. I have tried making it in Lua and i'm not to Successful
I'm not able to make everything like that, and i also wanna have my own custom Syntax Highlighting but choose the colors like this
Syntax = {
log/Log/LOG = Blue; -- Print Function
bringback = Red; -- return function
}
function ByteText(...)
string.Byte(...)
end
function CustomFunctionsName(data)
Do(Data)
end
So i'm kinda successfull in this but i don't want it running of Lua i don't mind using a Lua api or c# or python api but i just want to learn how to make it customly like how did python and LOLCODE make their own languages? I wan't to make it like that LOLCODE's functions are so random and i wanna make it Customly and my own way just like them, If someone can Explain that would be amazing <3
You don't say why you'd want to write your own language when there are plenty available, but to answer your question....
Start by searching "language parsers" in your favorite search engine.
Learn about language parsing itself. If you're still interested after that (and it isn't an easy topic if you intend to write something from scratch) you should be able to figure out where to go next.
P.S.
Your first worry is only about what your language will accomplish and what grammar it will support. Stuff like syntax highlighting isn't something to worry about until after your language is written and can compile itself.

Where is Ruby code, generated via metaprogramming, stored, and is it viewable/printable?

I've just started learning about metaprogramming in Ruby, and found myself wondering if it was possible to view (in someway) the code that had been generated. I want to, as a coding exercise, write a short method that will generate a Ruby file containing either a few method definitions or, ideally, an entire class or module definition.
I was thinking that perhaps just building up a string representation of the file and then merely writing it out might be a way to accomplish that, but that way doesn't really necessitate the use of metaprogramming, and since my goal is a metaprogramming exercise, I would like to figure out a way to incorporate it into that process or else do it another way.
I guess, if I was to take the string-building approach, I would like to start with something like
klass_string = "class GeneratedClass\n\t<BODY>\nend"
and then somehow save the output of something like this
define_method( :initialize ) do
instance_variable_set("#var", "some_value")
end
in a string that could replace '' in klass_string and then written out to a file. I know I could just put the above code snippet directly into the string, and it would workout fine, but I would like to have the output in a more standard format, as if it'd been written by hand and not generated:
class GeneratedClass
def initialize
#var = 'some_value'
end
end
Could someone point me in the right direction?
I agree with your comment that this question isn't really about metaprogramming so much as dynamic code generation / execution and introspection. Those are interesting topics, but not really metaprogramming. In particular your question about outputting ruby code to strings is about introspection, where as your string injection question is about dynamic code (just to try give you the words to google about what you're interested in).
Since your question is general and really around introspection and dynamic code, I'm going to reference you to some canonical and useful projects that can help you learn more..
ParseTree & Ruby Parser and Sourcify
Ruby Parser is a pure ruby implementation of ParseTree, so I'd recommend starting there to learn how to examine and "stringify" Ruby code. Play around with all of those, and in particular learn how they examine code in Ruby to generate their results. You'll learn a ton about how things work under the hood. Eric Hodel among others is real smart about this stuff.. Be warned though, this is really advanced stuff, but if that's where you want to build expertise, hopefully those references will help!

Rspec test case for array method

I am a fairly new to programming (3 months in), and am attempting to learn via TDD.
Obviously the point to TDD is write the test cases first, this particular piece I wasn't sure how to.
The code snippet is:
class PhraseFactory
def initialize
#sentence = ''
end
def make_sentences_from
for i in 0 ... self.length
#sentence += self[i] + ' '
end
end
How I was thinking to test it was using:
describe "When sent a message(<< is that proper terminology?) from an array of strings"
it "Builds a sentence"
my_word_array.should_have (here is where I am unclear)sent_a_message_to(make_sentences_from)
Thanks for any help.
i like TDD but i would not recommend anyone to use TDD to learn something new! it's always a good idea to use the REPL (irb) to experiment with code.
your example is full of WTFs for any ruby developer:
you are missing all the ENDs (looks kinda like python?!)
you are naming something Factory (are you a java guy?)
you use for instead of each
you are doing stuff in a class that should be a oneliner
you reinvent the wheel by rebuilding core functionality
besides that, i don't really understand your question and code...
what should the result of your code be? what is the input to your "factory"
$ irb
> %(you can just use join to build a sentence from an array of words).join
"you can just use join to build a sentence from an array of words"
Learning both a new language (Ruby) and technique (TDD) at once may be a bit too much. On the other hand, I find unit tests a great way to clarify code behavior, and as such a good learning tool. One suggestion here would be to look into something like the Ruby Koans: http://rubykoans.com/
I am not a Rubyist, so I can't comment on their quality, but I used the F# Koans, which were adapted from the Ruby ones, and were pretty good. This should both give you a good entry point into the language, as well as a familiarity with unit testing, which should serve you well once you start working on your own project and get into TDD.

Is there a doc like perlguts/illguts for ruby?

I recently enjoied the illustrated perlguts as fun and easy way to see how things are implemented without the need to dig through sources and would love to read such documentation for ruby.
Perlguts provides some information on the workings of the Perl interpreter and describes how to use the Perl API. Illguts i an illustrated version of perlguts.
Are there similar docs for ruby?
Here is some stuff along those lines:
The Ruby Hacking Guide (old, but contains some of that nuts & bolts info)
http://dev-logger.blogspot.com/2008/06/ruby-internals-by-patrick-farley.html

Ruby in Erlang

Is it possible to embed Ruby into Erlang? Would Yecc be the correct tool for that? If it is possible how many man hours would it take, rough estimate?
Erlectricity exposes Ruby to Erlang and vice versa:
http://github.com/mojombo/erlectricity
There is something called Elixir by Jose Valim url http://elixir-lang.org/ this lets you write code that is ruby like and run it on erlang VM.
So code looks similar in many places like:
iex> name = "world"
iex> "hello #{name}"
"hello world"
in many is more Erlang'y:
# function
iex> x = fn(a, b) -> a + b end
#Fun<erl_eval.12.111823515>
iex> x.(1, 2)
3
and modules =) :
iex> defmodule Math do
...> def sum(a, b) do
...> a + b
...> end
...> end
iex> Math.sum(1, 2)
3
It that helps you in any way. I know Jose recently started again massive work on it after he stopped to focus so much on Rails. Probably he will get Riak integration and some sort of a web framework. That could be interesting!
You need to explain in a little more detail what you want to do. "Embed" is a rather vague word in this context.
Yecc would be appropriate if you intended to implement a Ruby-parser/interpreter in Erlang, but I'm guessing this is not what you want to do.
If you want to call Ruby-code from Erlang, this can be done in a manner similar to how Erlang's jinterface application is used to talk to Java. (You can also google on the subject.)
You cannot expect to get any sort of realistic estimate without putting in a week or two of work, figuring out exactly what should be done and how. Otherwise, you'll end up with "anything from 2 months to two years", which probably isn't very helpful.
Depending on exactly what your task is, you could could do something as simple as spawning a ruby process to talk to via STDIN/OUT.
If you wish to evaluate some ruby code inside Erlang, you should prepare to spend about one year to launch simple code.
Ruby syntax is very complicated and erlang has a veeery different VM.

Resources