Hi guys.
I wanna learn how to write a new gem by myself (I'm newbie).
So I found some document to do it but it's quite hard for me.
I want to write a gem. So when I install it, I can use it on terminal, for example:
$ search key_work
But all document just said that I have to use:
$ irb -Ilib -rMygem
And then I can use it with Mygem.search.But it's not my purpose.
My question is "How I can write a code for key-word that can run on terminal?"
Thanks for your help :)
You might not want to make a gem just yet. what you want - I think - is to make an executable. This does not require a full GEM.
I'll be making 2 assumptions here, you are on a UNIX system, and ~/bin is in your PATH.
First make a file with the name of your program (for this example call proga) in your ~/bin folder.
Start this file with (only first line matters)
#!/usr/bin/env ruby
# your code here
# p gets.strip
We just want to make it executable: chmod +x ~/bin/proga
If you really want to make a command line gem, you can find a good start here: http://robdodson.me/how-to-write-a-command-line-ruby-gem/ and http://blog.excelwithcode.com/build-commandline-apps.html
Related
I'm a newbie trying to train my self using ruby on rails. I am trying to follow an instruction where I am require to type in "subl Gemfile" on Git Bash but it constantly keeps showing 'sh.exe": subl: command not found'. Kindly advice the way forward.
P/S: I am a beginner in programming
go to the directory where you're creating your RoR app.
suppose you're creating your app at
/Users/LocalUser/workspace/YourApp
Then, there typing subl Gemfile would work
first type pwd in your command terminal and check where are you right now
also if you're running it from there, then try following link solution
https://danlimerick.wordpress.com/2014/01/07/git-for-windows-tip-opening-sublime-text-from-bash/
I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (except for the method calls).
I know I can see the source from the Github repository but I would like to see it in my editor and on my local machine.
I'm guessing this code must be in some main Ruby directory, but I'm having trouble locating it. Any help is appreciated. Thanks.
Besides Sergio's suggestion, there is another option.
Within your Rails path
$ bundle open devise
This will open the installed gem in editor with the version specified in Gemfile, very handy.
Try gem unpack, it will copy source of a gem to current directory. For example,
gem unpack rails
Documentation: gem unpack.
Simply run bundle show <gem-name>,
it will list the absolute path of gem source code and in next step simply open source code using text editor like this
subl <gem-code-absolute-path>
For Example
Let's assume you want to read kaminari gem code
bundle show kaminari
/home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
next step (subl is command to open with Sublime Text Editor)
subl /home/abdullah/.rvm/gems/ruby-2.3.0#your_gem_name/gems/kaminari-0.16.3
Run gem environment - this will display you all the information about your gems, including their location.
Additionally I would advise you to install some IDE with go to source feature - RubyMine is just brilliant (and has 30-day-long free trial), if you want to go for absolutely free go with NetBeans together with Ruby plugin. This feature allows you to navigate quickly to source of clicked method, regardless whether it is defined inside your code or inside the gem.
Clone the github repo in your local machine and explore it using your prefered editor:
git clone https://github.com/plataformatec/devise.git
As the title says, how to use luadoc in ubuntu/linux? I generated documentation in windows using batch file but no success in ubuntu. Any ideas?
luadoc
Usage: /usr/bin/luadoc [options|files]
Generate documentation from files. Available options are:
-d path output directory path
-t path template directory path
-h, --help print this help and exit
--noindexpage do not generate global index page
--nofiles do not generate documentation for files
--nomodules do not generate documentation for modules
--doclet doclet_module doclet module to generate output
--taglet taglet_module taglet module to parse input code
-q, --quiet suppress all normal output
-v, --version print version information
First off, I have little experience with Luadoc, but a lot of experience with Ubuntu and Lua, so I'm basing all my points off of that knowledge and a quick install that I've just done of luadoc. Luadoc, as far as I can see, is a Lua library (so can also be used in Lua scripts as well as bash). To make documentation (in bash), you just run
luadoc file.lua
(where file is the name of your file that you want to create documentation for)
The options -d and -t are there to choose where you want to put the file and what template you want to use (which I have no clue about, I'm afraid :P). For example (for -d):
luadoc file.lua -d ~/Docs
As far as I can see, there is little else to explain about the actual options (as your code snippet explains what they do well enough).
Now, looking at the errors you obtained when running (lua5.1: ... could not open "index.html" for writing), I'd suggest a few things. One, if you compiled the source code, then you may have made a mistake somewhere, such as not installing dependencies (which I'd be surprised about, because otherwise you wouldn't have been able to make it at all). If you did, you could try getting it from the repos with
sudo apt-get install luadoc
which will install the dependencies too. This is probably the problem, as my working copy of luadoc runs fine from /usr/bin with the command
./luadoc
which means that your luadoc is odd, or you're doing something funny (which I cannot work out from what you've said). I presume that you have lua5.1 installed (considering the errors), so it's not to do with that.
My advice to you is to try running
luadoc file.lua
in the directory of file.lua with any old lua file (although preferably one with at least a little data in) and see if it generates an index.html in the same folder (don't change the directory with -d, for testing purposes). If that DOESN'T work, then reinstall it from the repos with apt-get. If doing that and trying luadoc file.lua doesn't work, then reply with the errors, as something bigger is going wrong (probably).
I know there is no direct support for Ruby gems in the Rhomobile framework. I have read their (sparse!) documentation to migrate in gem support through extensions, but I cannot for the life of me figure out how exactly this should be implemented.
Besides the document linked above being very disjointed, what I can find can't be easily translated to what I need. I am trying to bring devise into my app, but the gem structure is very hierarchical and the example given in the Rhomobile documentation suggests that a given library should be a singular .rb file.
The exact example given is as follows:
Assuming your application is called “mynewapp”, create a directory
under app called lib (or whatever you wish to call it):
$ cd mynewapp
$ mkdir app/lib
$ cp /path/to/my_lib.rb app/lib/my_lib.rb
Then just require lib/my_lib in a given file within my app. Eg:
require 'lib/my_lib'
To translate to the devise gem, my assumption is that I couldn't do something similar, but would instead have to flatten the directory structure out in some way before I could use it. Is that the case or am I missing something? That's a lot of re-writing code...
Also, if anyone knows of any kind of guide to adding gems to the Rhodes framework, I would love to see it! I've looked through most of the official documentation and some non-official and nothing seems to address this at all.
Wow. I don't know how I overlooked this, but it's really simple and nothing like what I was assuming.
If you are using Rhodes via the RubyGems installation, you must add external Ruby libraries to your RubyGems installation directory for the ‘rhodes-framework’ gem. Your RubyGems installation directory can be found with gem env in a terminal.
From the same page linked in question.
My paths didn't match what was listed in that document because I'm using RVM, but I just ran find / -name rhodes-* and just looked for the one followed by /lib/framework.
I am attempting to take a whack at creating my first Rails application template and I am running into a slight issue with the copy_file method.
First some background.... Apparently the Ruby OpenSSL package does not ship with a CA store, so any attempt to connect to an HTTPS service will fail out of the box. The way around this(for Rails 3 apps) is to add the line OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE to the top of your config/environment.rb file. I need to do this on the fly in my template so I can install jQuery.
So I have that all figured out, my general thought is to:
Make a backup of my config/environment.rb file.
Prepend the data to original
Run the jquery:install --ui task
Restore the original config/environment.rb file.
See my template Gist, Lines 25..34 is the relevant section.
So all of that works until step #4 which fails with Error: Could not find "env.orig" in any of your source paths on line #31.
This is VERY perplexing to me because line #28 works, I can see the env.orig file on disk, so why won't the reverse work?
What am I doing wrong?
Update 1:
After looking at the Thor source thor\actions.rb it became clear that Thor uses different paths (not your current project path) for the source and destination. Furthermore my copy was actually not working, it was actually coping the ERB template file, not the already generated file.
After a breather it occurred to me use the right tool for the job so now I have: run 'cp environment.rb environment.~' and run 'mv environment.~ environment.rb' which works just fine. I am fairly certain this would not work on a windows box without the unix tools installed, but I can live with that. Does anyone have a better way?
See my Update for a Why, but the solution was to use the right tool for the job so now I have: run 'cp environment.rb environment.~' and run 'mv environment.~ environment.rb' which works just fine. I am fairly certain this would not work on a windows box without the unix tools installed, but I can live with that.