I have been a Rails developer for a year now, and I really want to start diving in to Ruby and pure programming without using a framework. However I am having trouble conceptualizing little programs I can build on my own. I guess it's because I'm so used to having a GUI (the web browser) to interact with the classes that I write that I don't really get how I would interact with classes without clicking, going to different pages, etc.

In terms of pure Ruby (without Rails), I've only built a blackjack game you play through the console through typing Y/N for hitting and staying. I would like to know what types of interfaces you guys use for your small programs? Are they all just ran once from your console using something like ruby myclass.rb? I have the motivation but I don't know what I would do after writing a few classes. All I do is have a separate file that requires all my other classes, instantiate my classes like so:

myclass = MyClass.new
myclass.part = ClassPart.new(1,2,3)
myclass.do_something # puts "value of instance_variable = #{@instance_variable}"

And then what? The program finishes. It's not interactive. It doesn't do much. I don't see myself building anything cool/powerful like that. Can anyone enlighten me?

closed as unclear what you're asking by user40980, GlenH7, Kilian Foth, ChrisF Jan 19 '14 at 22:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What is the actual question? :) – Andres F. Jan 15 '14 at 16:57

There are many ways you can run a program and do 'something'(obviously). Since you come from Rails one simple alternative would be to write a web service that takes a few params and returns simple json. That way you could somehow stop thinking about minor details of rendering and displaying results.

Next option would be to read some text from the commandline and simply output text as result. There have been many Basic games of this kind around back in the 80s. That's still a nice way to learn those simple things.

Or just solve some of the Project Euler problems. Those normally require a script with no special input (some read data from files) and get a single number as result that you just print on the screen. Many Code Katas and similar small code challenges work this way.

Btw: even in real live you often write such scripts. A large part of my current Rails project are various scripts for importing/exporting and processing data. (Not your average Rails project)

And then there is of course the option to go 'full GUI'. There are several toolkits and libraries for that. Maybe write a simple game or whatever you like.

  • So for your current Rails project, do you just have a few file field tags and when the user uploads a file you use a ruby class to process it? So you don't really have any models that use activerecord? – Edmund Dec 23 '13 at 15:14
  • @Edmund the project is a web service with normal controllers & models. Though it only returns JSON or XML, so not much of views (there is an admin interface). The scripts I mentioned are there to exchange data (categories, products, orders...) from and to our ERP system (MS-SQL Server, plain SQL) or from web apis or EDI systems of some of our customers or distributors. So a lot of different systems, sometimes processing files, sometimes JSON reponses and others. – thorsten müller Dec 23 '13 at 22:16

The classes is just small or not small entities, that implements a defined set of functions. That functions can be realized by single or multiple set of methods. Mostly ruby scripts is used as console applications, which bring to mind console linux application like: ls, true, etc. And ruby apps also can be run by cron to perform daily work, like backup or etc, and you can automize some of you work on your PC by creating a simple application that you be able to use manually or by run from that cron.

Usually that application replesents a so called ruby gem, and is reshared with open source license. List of gems you can see at rubygems.org site.

For example, you can write an application that...., then turn it into a gem, and publish it. I'll describe steps do have to do from a scratch:

  1. Install rvm.

  2. Install some ruby version, for example any revision of ruby 2.0.0:

    $ rvm install ruby-2.0.0
  3. Install a gem bundler:

    $ gem install bundler   
  4. Generate a gem sketch with bundler:

    $ bundle gem yourgem $ cd yourgem

  5. Edit .gemspec, and Gemfile accoring your needs.

  6. Install all required gems:

    $ bundle install
  7. Define your class in the /lib folder of your gem:

    $ vim lib/yourgem.rb
  8. Define your binary (if needed) in the /bin folder of your gem:

    $ vim bin/yourgembin.rb
  9. Change your gem version:

    $ vim lib/yourgem/version.rb
  10. Release your gem with rake. it publishes the gem into rubygems:

    $ rake release
  11. For example install the gem for your system:

    $ sudo gem install yourgem
  12. Use it:

    $ yourgembin
  13. If you wish to contribute to open source community you can pushlish you gem source into github.

Also you you are not forced to accomlish the all f the points. it is the sketch only.

Of course if you are the beginner of ruby you shell to think about not only valid syntax during the program write, but you shell to think about usability, and readability of your code syntax also. The good style you can find here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.