Wednesday, 29 June 2016

Ruby Script

1) Open website in chrome using ruby script in windows


`start http://www.google.co.in`


------------------------------------------------------------------------------


2) creating directory

directory_name = "sree"
Dir.mkdir(directory_name) unless File.exists?(directory_name)


------------------------------------------------------------------------------


3) open cmd using script


require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('cmd.exe', '', '', 'open', 3)




____________________________________________________________________________



4) running cmd commands using ruby


require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute("cmd.exe", "/c time 9:30:00 PM", "", "runas", 1)




-------------------------------------------------------------------------------------------------------------------------


5) Reading file


File.open('New Text Document.txt', 'r') do |f1|  
  while line = f1.gets  
    puts line  
  end  
end  


------------------------------------------------------------------------------------------------------------------------



6) open text file in cmd line


i) start sree.txt

  ii)  start c:\Users\mobigesture\Desktop\sree.txt

-------------------------------------------------------------------------------------------------------------------

7) opening txt file in browser using cmd line

@echo off
start chrome "c:\Users\mobigesture\Desktop\sree.txt"

_________________________________________________________________________


8) open app using cmd line

@echo off
start "__App_Name__" "__App_Path__.exe"

@echo off
start "Sticky Notes" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Sticky Notes"



--------------------------------------------------------------------------------------------------------------------------

http://ruby.bastardsbook.com/chapters/io/



9)

creating a file and writing to it-> save as  file.rb -->  open cmd--> run like -> ruby file.rb


fname="sample.txt"

f= File.open(fname,"w")
f.puts "hey this is test file"
f.close


(or)


f=File.open("sample.txt","w")
f.puts "hey welcome"
f.close


(or)  ->>>  if file is not exist it creates a new file


File.open("a.txt","w"){|f|

 f.puts("apple !!")


}

--------------------------------------------------------------------------------------------------------------------------
http://ruby.bastardsbook.com/chapters/io/


10) Installing gem files in ruby

gem install rest-client


require 'rubygems'
require 'rest-client'

wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"

File.open(wiki_local_filename, "w") do |file|
   file.write(RestClient.get(wiki_url))
end

--------------------------------------------------------------------------------------------------------------

11) creating , deleting, writing into file in cmd



i)  creating file (-> dot is required after echo)


echo. >ruby.txt


ii) opening file

start ruby.txt


iii) deleting file


 del ruby.txt


-------------------------------------------------------------------------------------------------------------------------


12)  Reading from file (create file before running)

f=File.open("ruby.txt","r")
lines=f.read
puts lines


(or)

lines = File.open("ruby.txt", "r"){ |file| file.read }
puts lines


--------------------------------------------------------------------------------------------------------------

13) using Readlines & Readline


File.open("ruby.txt").readlines.each do |line|
puts line
end



(or)

file=File.open("ruby.txt","r")
while !file.eof
line=file.readline
puts line

end




---------------------------------------------------------------------------------



14)match line in array of strings



lines = ["Hello world", "  How are you?", "*Fine*, thank you!.", "  OK then."]   
lines.each do |line|
   puts line if line.match(/^  H/)
end


(or)


lines = ["Hello world","Hey", "  How are you?", "*Fine*, thank you!.", "  OK then."]   
lines.each do |line|
   puts line if line.match(/^H/)
end



o/p:
Hello world
Hey


--------------------------------------------------------------------------------------------------------------


15) closing files


file=File.open("ruby.txt","r")
lines=file.readlines
file.close

lines.each {|line| puts line}



 (or)


lines=File.open("ruby.txt","r") {  |file|
file.readlines
}

lines.each{ |line| puts line}


------------------------------------------------------------------------------------------------------------------

16) check if file exist or not--->  checks in present directory


if File.file?("ruby.txt")
puts "yes"
else 
puts "no"

end


(or)


if File.exist?("ruby.txt")
puts "yes"
else 
puts "no"


end


-------------------------------------------------------------------------------------------------------

17)creating directory


dirname = "data-files"
Dir.mkdir(dirname) unless File.exists?dirname

















========================================================================




Tutorials


1)



Launching Apps and Printing Docs with the Windows Shell

A reader recently asked how to launch an application from within a Ruby script. A quick answer is to use the system or execmethods. But you can also leverage the Windows Shell to launch applications, and have control over the window state. You can also use the shell to print documents. Let's get right down to it, shall we?...

Require the win32ole library...
require 'win32ole'

Create an instance of the Windows Shell object...
shell = WIN32OLE.new('Shell.Application')

The shell object's ShellExecute method performs a specified operation on a specified file. The syntax is...
shell.ShellExecute(FILE, ARGUMENTS, DIRECTORY, OPERATION, SHOW)

FILE: Required. String that contains the name of the file on which ShellExecute will perform the action specified by OPERATION.

ARGUMENTS: Optional. The parameter values for the operation.

DIRECTORY: Optional. The fully qualified path of the directory that contains the file specified by FILE. If this parameter is not specified, the current working directory is used.

OPERATION: Specifies the operation to be performed. It should be set to one of the verb strings that is supported by the file (Examples: 'open', 'edit', or 'print'). If this parameter is not specified, the default operation is performed.

SHOW: Recommends how the window that belongs to the application that performs the operation should be displayed initially (0 = hidden, 1 = normal, 2 = minimized, 3 = maximized). The application can ignore this recommendation. If this parameter is not specified, the application uses its default value.

So, to launch Excel in a maximized window...
shell.ShellExecute('excel.exe', '', '', 'open', 3)

I suppose you could also launch your rails app with something like this...
shell.ShellExecute('ruby.exe', 'c:\my_rails_app\script\server', '', 'open', 1)

To print a document, hiding the application window...
shell.ShellExecute('C:\MyFolder\Document.txt', '', '', 'print', 0)

That's about it. As always, post a comment here or send me email if you have questions, comments, or would like to request a topic for discussion.

Thanks for stopping by!

Monday, 27 June 2016

Revert commit from Git hub


git reset --hard b23fc9455481f3c3040271a2f1e3bca7933ea635

git push -f





======================================================

git reset --hard head (commit id)

git push -f



=============================================

mobigesture@sree MINGW64 /d/turkish IBT/vh_ewt/IBT (Attaching_vh_ewt)
$ git log
commit c37bcbafc206c7269c01400a938d25ca2de7916d
Author: srinivas <srinivas.panaganti@mobigesture.com>
Date:   Tue Jul 12 16:02:35 2016 +0530

    VH_EWT added in GVP and AVPIDriver

commit b23fc9455481f3c3040271a2f1e3bca7933ea635
Author: srinivas <srinivas.panaganti@mobigesture.com>
Date:   Tue Jul 12 11:54:59 2016 +0530

    Attached VH_EWT


=================================================================

mobigesture@sree MINGW64 /d/turkish IBT/vh_ewt/IBT (Attaching_vh_ewt)
$ git reset --hard b23fc9455481f3c3040271a2f1e3bca7933ea635
HEAD is now at b23fc94 Attached VH_EWT

mobigesture@sree MINGW64 /d/turkish IBT/vh_ewt/IBT (Attaching_vh_ewt)
$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/VHT/IBT.git
 + c37bcba...b23fc94 Attaching_vh_ewt -> Attaching_vh_ewt (forced update)


==============================================================

Friday, 24 June 2016

find out MySQL URL, host, port and username


If you're already logged into the command line client try this:
mysql> select user();
It will output something similar to this:
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.41 sec)
In my example above, I was logged in as root from localhost.
To find port number and other interesting settings use this command:
mysql> show variables;
======================================================================

SHOW VARIABLES WHERE Variable_name = 'port';


mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.

If you want to know the hostname of your Mysql you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'hostname';


mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| hostname          | Dell  |
+-------------------+-------+
1 row in set (0.00 sec)
It will give you the hostname for mysql.

If you want to know the username of your Mysql you can use this query on MySQL Command line client --
select user();   


mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
It will give you the username for mysql.



=======================================================================


Install mysql console from chrome store.



-> open mysql console

1)login localhost 3306 sree 1234


sree@localhost:3306>show databases;

======================================================================

find out MySQL URL, host, port and username


If you're already logged into the command line client try this:
mysql> select user();
It will output something similar to this:
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.41 sec)
In my example above, I was logged in as root from localhost.
To find port number and other interesting settings use this command:
mysql> show variables;
======================================================================

SHOW VARIABLES WHERE Variable_name = 'port';


mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.

If you want to know the hostname of your Mysql you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'hostname';


mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| hostname          | Dell  |
+-------------------+-------+
1 row in set (0.00 sec)
It will give you the hostname for mysql.

If you want to know the username of your Mysql you can use this query on MySQL Command line client --
select user();   


mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
It will give you the username for mysql.



=======================================================================


Install mysql console from chrome store.



-> open mysql console

1)login localhost 3306 sree 1234


sree@localhost:3306>show databases;

======================================================================

Installing mysql on windows 64 bit and using it with Ruby


Recently I reinstalled by entire Windows installation due a SSD migration and wanted to take the opportunity to document the configuration process, specially in the light of the known issues with making it work with Ruby.
It is important to mention that most of these issues in Ruby land are generated due the lack of documentation associated on how to properly install MySQL and its related dependencies.
Most of us expect things to Just Work, and we often compare the installation process to installing to Linux orOSX which works out of the box.
What is not mentioned in the documentation installation is that development tools (a compiler) and specific headers and libraries are required. Unless you’ve already installed them, these development artifacts are usually missing from a Windows computer.
What I’m going to describe next is how I installed and configured my environment and how I made it work properly. YMMV if you decided to use different versions of the components I used for this.
Let’s get started:

As I mentioned before, I’m using a 64bits version of Windows, so I think it will be best if I download a matching bits version.
So went ahead and visited MySQL download site:
And selected MySQL Community Server 5.5.13.
From the versions offered, downloaded Windows (x86, 64-bit), MSI Installer

Invoked the installer and presented with the normal installation wizard.
You can follow the next sequence of images to use my same settings.
Is good to always install your Data files outside the default program installation directory, that way, you can safely upgrade your installation and not to worry about an installer removing your data files.
Ok, so let’s move to the next thing…

Update: mysql gem version `2.9.0` already fix the issues shown here. Install it normally and follow the on-screen instructions.
In order to use my brand new MySQL installation, now I need to install the MySQL bindings for it.
But, there is a small detail: Ruby is 32bits and my MySQL is 64bits, this means I can’t use MySQL provided libraries from Ruby.
Bummer! You told me to install the 64bits version!
Don’t despair! MySQL Connector to the rescue!
That is right, MySQL has something called Connector, the purpose of that library is to avoid a complete MySQL installation when you just need to connect to a remote one.
It comes in different flavors, we are interested in C language support, since that is the language Ruby uses for it’s extensions.
We are going to download a 32bits connector and use it!
So, at my web browser again, decided to visit the MySQL Connector/C download page:
Since I’m not interested in installing this Connector and pollute my clean 64bits installation, I’m going todownload the non-installer version.
I scrolled down the listing until I saw the noinstall 32bits version:
mysql-connector-c-noinstall-6.0.2-win32.zip
Decided to extract it to the root of my disk, so I ended with a folder named mysql-connector-c-noinstall-6.0.2-win32 in there.
Remember: extract into a folder without spaces. The same goes for your Ruby installation and the DevKit installation.

So, now that all MySQL prerequisites are in place, will open a new command prompt and prepare to install the gem.
This time I’m going to use Ruby 1.9.2, properly installed and configured with the complementary Development Kit (DevKit) which is provided at RubyInstaller website (In case you haven’t installed yet, don’t forget to follow the installation instructions in the wiki)
OK, so in a Command Prompt, will type the gem installation command:
gem install mysql --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32
Note the use of forward slashes for the directory where MySQL Connector/C was extracted.
The above command contains two special things:
First, we are telling RubyGems that we want the ruby platform of mysql gem. This particular platform is the one that contains the source code and this will allow us to skip the pre-compiled version of the gem.
The second part, which is added after two dashes, are the additional arguments that we are giving to the gem configuration process to locate our MySQL headers and libraries for successful compilation.
As result of this command, you will see something like this:
Fetching: mysql-2.8.1.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Which indicates the gem installed successfully.
In case you obtained a different result, please refer to RubyInstaller Troubleshooting page:
https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting
And try the proposed solutions there.

Now that we installed the gem, we can remove the connector folder we first extracted. Before do that, first we need to take out a file from there: libmysql.dll. This file is required by the gem we compiled and needs to be available for it.
You can find it inside the lib directory of MySQL Connector.
I personally recommend you place it along your Ruby installation, inside the bin directory.
If you have multiple Ruby installations and you use Pik to change between them, you can place the library in the same directory Pik is installed. You need to remember that it is important the libmysql.dll file is on the PATH when you need to use it.
OK, after all that big red warning, let’s test this thing on a IRB console:
irb> require "rubygems"
irb> require "mysql"
irb> conn = Mysql.connect "localhost", "root", "abc123"
irb> result = conn.query "SELECT 1"
irb> result.num_rows
=> 1
irb> result.fetch_row
=> ["1"]
irb> result.free
irb> conn.close
irb> exit
Great!, now you have not just a working MySQL installation but also Ruby configured to talk to it!
Hope you enjoyed this post as I did enjoy creating it. Hope this ease your path on using MySQL with Ruby on Windows.

====================================================================================
http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/
======