ribdev is shutting down.

It’s been nearly a year now since my last post, It’s been a tough year.
I’ve made my decision after a long time of thinking and reconsidering the available options, and I’ve decided to end ribdev, but every end is a new beginning.”

Amr Essam.

Advertisements
ribdev is shutting down.

RING – How to download files?

Downloading files using the RING is very simple, just by using the DOWNLOAD() function.
SYNTAX:

download(url) #OUTPUT: STRING

And for saving the file, We’ll use the WRITE() function.
SYNTAX:

write(file, content)

The Code Example:

see "Please enter a url to download: " give URL
if not isNULL (URL)
   content = download(URL)
   if not isNULL (content)
      see "File is downloaded, please enter the file name: " give filename
      write(filename, content)
   ok
ok

In the last example, we let the user determine the name of the file, but we will let the program do it by itself, this may be harder.

We can split the url: (Thanks to Gal Zsolt):

list = str2list(substr(trim(url), "/", nl))
reversedList = reverse(list)
filename = reversedList[1] 
see filename #OUTPUT

Or, we can do it manually:

First, We will reverse the url, then we will detect the position of the ‘/’ character from the reversed url, after that, we will use the right() function to cut out the name of the file from the original url from the right direction.

I hope you don’t get lost, but all of this is explained below.

Creating the reverser function:

#SYNTAX: func func_name parameter, parameter, ...
func string_reverser str
	new_str = ''
	for i = len(str) to 1 step -1
		new_str += str[i]
	next
	return new_str

Creating the function to extract the filename:

#SYNTAX: func func_name parameter, parameter, ...
func getURLFileName url, reversedUrl
	postion = substr(reversedUrl, '/') #returns the position of the last '/' character in the original url, which is the first in the reversed one.
	file_name = right(url, postion - 1)
	return file_name

Then we will have the final code like this:

#----------Downloading Section----------
see "Please enter a url to download: " give URL
if not isNULL (URL)
   content = download(URL)
   if not isNULL (content)
   	  reversedURL = string_reverser(url)
	  filename = getURLFileName(url, reversedURL)
      write(filename, content)
   ok
ok

#----------Parsing Section----------
func string_reverser str
	new_str = ''
	for i = len(str) to 1 step -1
		new_str += str[i]
	next
	return new_str

func getURLFileName url, reversedUrl
	postion = substr(reversedUrl, '/')
	file_name = right(url, postion - 1)
	return file_name

I hope you understand the code.

I prefer to let the user determine the file name like in the first example because some URLs doesn’t clearly contain the filename which will cause errors.

amrESSAM.

RING – How to download files?

Ring Documentation Walkthrough #14 final – Eval, Error handling

Here you’re in the final chapter in the tutorial.

This is very easy chapter to understand.

We’ll learn first Try/Catch/Done:

try
see x = 5
catch
see 'error' + nl + cCatchError #we use this variable to get the error message
Done

Eval:

eval('x = 5')
see x #output: 5
eval('x = 1*3')
see x #output: 3
eval("func printme see 'print me please.'")
printme()

Raise:

We can use this to raise an exception.

raise('error: blah blah has just happened right now in the front of your eyes.')

Assert:

Official example : here
We can use the Assert() function to test conditions before executing the code,
If the test fail the program will be terminated with an error message contains the assert condition.

x = 10

assert (x = 10) #will pass the test
assert (x = 5) #won't pass the test, output: Line 3 Assertion Failed!

This is the end of this tutorial, of course it’s not the end of the official document, there’s more advanced stuff there, but i don’t want to continue through the rest of the document just like that, i need to parctice more on using the Ring first.

I will make new tutorials after a while.

Till that, i will be posting some ring codes, news, tools & add-ons and other stuff in the blog.

See you in the next post 🙂

amrESSAM.

Ring Documentation Walkthrough #14 final – Eval, Error handling

Ring Documentation Walkthrough #13 – System Functions

Welcome to the new chapter ‘System Functions‘, this is a very easy chapter.

our functions:

  1. system() – exceute a system command.
  2. get() – get environment variables like PATH, TEMP .. etc
  3. isMSDOS() – check if the operation system is msDOS or not, returns 1 if true, 0 if false
  4. isWindows(), isWindows64(), isUnix(), isMacOSX(), isLinux(), isFreeBSD(), isAndroid() – all returns 1 if true, 0 if false
  5. windowsNL() – returns the windows’ new line string that contains : CR+LF { char(13) + char(10) }

  6. sysargv – it’s a list contains the command line parameters.

    see sysargv

  7. filename() – returns the active script file name.


This is pretty much what we have in the chapter.

see you in the final one.

amrESSAM.

Ring Documentation Walkthrough #13 – System Functions

Ring Documentation Walkthrough #12.1 – Advanced Read & Write Files

Hello, We’ll go through some advanced operations about the files.

File Handling:

We will learn in the next example these functions:

  • fopen – open a file
  • fgets – read a specific amount of a file
  • fgetc – read a character of a file
  • fexists – check if a file exists
  • freopen – re-open a file in a file handler
  • fseek ( file_handle, offset, whence – set the position of the stream, whence: ( 0=beginning of the file, 1=current position, 2=end of the file)
  • rewind – set back the position to the beginning of the file
  • fgetpos & fsetpos – get handle of the current position and set new position for the file
  • fputc – write a single character
  • fputs – write a text
  • fwrite – write a text
  • fread – read a specific amount of a file
  • ftell – know the current position
  • fclose – close the file handler

/*
r - read
w - write new or overwrite
a - appends and creates new if doesn't exists
r+ - read & write (update file)
w+ - read & write
a+ - read & append
*/
file = fopen('somewhere.txt', 'r')
see file[1]
see fgets(file, 100) #the same result as above.

see fgetc(file) #returns a character or EOF

see fexists('some_other_file.txt') #returns 0 (false), 1 (true)

file = freopen('some_other_file.txt', 'r+')
fseek(file, 0 , 2) #go to the end of the text file
see ftell(file)
fputc(file, 'a') #append a character
fputs(file, 'amr') #append a text

rewind(file) #set back the position to the beginning of the file.

fwrite(file, 'consider this a something just for the example, ty.') #writes to the file

fread(file, 100) #returns 100 characters from the file

x = fgetpos(file) #returns position handle
fsetpos(file, 5) #set new position of the file
fclose(file) #close the file handler.

Temperory files:

There’re two functions for this.

x = tempfile() #returns a file handle for a temp file.
x = tempname() #returns a temp file name that doesn't exists

Errors:

  • clearerr

    clearerr(file) #clear the EOF error and the error indicators of a stream.
    
  • feof

    see feof(file) #returns 1 if it's the end of the file, 0 if it's not.
    
  • ferror

    see ferror(file) #returns 1 if there's error indicator, 0 if not.
    
  • perror

    perror('error happened') #print error message to the stderr.
    

fflush:

We can flushes the output buffer of a stream using the Fflush() function.

fflush(file)

ungetc:

We can push a character to the stream using this function so
the character will be available for the next read.

ungetc(file, 'a')

Well, at this point we’re done of the files chapter.

see you in the next chapter.

amrESSAM.

Ring Documentation Walkthrough #12.1 – Advanced Read & Write Files

Ring Documentation Walkthrough #12 – Files

Welcome to the new chapter, we will discuss today some of the files functions.

This part is easy to understand.

  • Read()
    This function basically reads the content in a file and returns it as a string.
    For an example:

    content = read('info.txt')
    see content
    content_exe = read('chrome.exe')
    see content_exe
    

  • Write()
    You can use this function to write string or binary files.

    content = 'Surrender'
    write('message.txt', content)
    

    We can using it in copying files too:

    pdf1 = read('ringdoc.pdf')
    pdf2 = write('ringdoc2.pdf', pdf1)
    

  • Dir()
    It basically returns all the files & sub directories found in a directory.
    It returns a list format, every item in the list is actually a list contains two items : 1- file/sub directory name, 2- Type (0 for File, 1 for Directoy)

    tree = dir('folder')
    for item in tree
       see item[1] #output: the file/directory name
       if item[2] = 0 see 'File' + nl
       but item[2] = 1 see 'Directory' + nl
       ok
    next
    

  • Rename()

    rename('tweeter.ico', 'twitter.png')

  • Remove()
    For deleting files.

    remove('setup.exe')
    

That’s it, next part i will discuss some of the left functions.

See you in the next one.

amrESSAM.

Ring Documentation Walkthrough #12 – Files

Ring Documentation Walkthrough #11 – Math

Welcome to the new Chapter, we’ll talk about the mathematical functions.

First of all, the list of the main functions are here, i won’t include them in the post because they’re easy to understand.

So, there’re 3 small functions left.

      1. Random()
        see random() #output: random number
        see random(100) #output: random number <= 100
        see random(10) #output: random number <= 10
        
      2. Unsigned()
        see unsigned(5, 10, '+') #output: 15
        see unsigned(5, 10, '*') #output: 50
      3. Decimals()
        see 5.12345 + decimals(3) #output: 5.123
        see 5.12345 + decimals(2) #output: 5.12
        

 


Yeah, This is all about the mathematical functions in Ring.

See you in the next chapter.

amrESSAM.

Ring Documentation Walkthrough #11 – Math