ruby

Ruby: Automating service start up in windows

In my windows 7 machine, I have a lot of services that I keep turned off. I turn them on only when I actually needed them. This keeps my computer working better than if I had all of the services running at the same time.

So every time that I need to turn them on, I have to manually go to the Services control and start each service. Pursuing the goal of attempting to automate as many repetitive tasks as possible, I wrote this small ruby script that, when one runs it as administrator (windowsese for sudo), it will start both of them up with one action and less time.

services = ['Apache2.2', 'MySQL']
 
services.each do |service|
 command = "net start #{service}"
 system command
end

DCRUG meeting bullet point summary

ruby icon

I just came back from the meeting, and as usual, it was pretty stimulating.

In the first presentation several good ideas were presented about refactoring:

* A method should only have 8 lines (six if you are good enough)
* Use presenter objects and avoid helpers
* Use more ruby and less rails
* Refactor ideas together as well as repeated lines of code

The idea of the presenter object is interesting. It is similar to the ViewModel found in .Net MVC or in MVVM. My understanding of the presentation is that it is a ViewModel on steroids, where some of the rendering occurs within the object. I like that idea, and it should be easily translated into .Net. There was no mention of a business layer. Maybe the idea is that business rules should be applied to the model? I will have to research that.

The second presentation had to do with using jruby to create a restful service on top of an existing java application. Some of the interesting ideas learned from this presentation are:

* Java code can run faster than C++ code
* Ruby's meta programming can reduce the amount of code dramatically, and make the code more readable
* Ruby's meta programming seem very similar to writing code and using eval in other languages, except that you are working on the objects directly
* They used cucumber to do integration tests. I really should learn about this.

Overall the presentation gave me ideas on practical applications of using jruby or ironruby.

An interesting factoid that was mentioned during the meeting is that a symbol in ruby is not destroyed by garbage collection. Once it is created, it will stay until the process ends.

Rough notes @ ruby nation: Keynote

Dr. Nic Williams

Rough notes.

InstantRails 1.8.6

Created RailInstaller
http://railsinstaller.org/

The problem is that as people become better at rails and ruby, they move to OS/X.

We should help others; we want to ruby to win.
Awareness to Advocacy
Novice to expert
User to Core Contributor

Corollary:
healthy community == huge, diverse, growing community

Rough notes @ ruby nation: Thinking Small

Ben Scofield

Notes from presentation.

Thinking small

Why not small? Because it was painful. But there are some good reasons.

Why small is better
Classes
Small classes
design
test
debug
replace
reuse
understand

SOLID
single responsibility principle
Interface segregation principle -- interface let you only do one thing
Demeter principle -- cannot add two apps. Don't dig too deeply in the internal classes

Applications
design
build
test
design
scale
replace
purchase

Monolith app == data injects to the rest of the classes
Small == the rest of the components request from the data app

Benefit: you can mock those components

Unicorn driven development
It is easier to create a small project.
If you don't ship, it doesn't matter.

Easy to start.
Easy to finish.
Motivating == Addicted to success.
Debugging

You can use the best app for the system.
Question: how would you communicate between the different apps?

Unix philosophy for web development.

How to do it?
* Very good integration tests
* Isolate
* Replace

Texticle gem: you can define indices, and you can name scopes depending on that.

New projects
* Integration tests
* Plan for modularity upfront
* Look for opportunities for smaller apps. See what others have already seen the need to seperate
* API, HTTP and REST
* Discoverability: you should see everything you could do with that resource.
* Mountable Apps

Think Small

Smaller Is Worse
* Latency
* Reliability
*

Rough notes @ ruby nation: GuardRails: a (Nearly) Painless Solution to insecure Web Applications

Guard rails

http://guardrails.cs.virginia.edu

It works by adding comments, and for guard rails use later.
Place the annotation near the target.

Context dependent policies.

Taint tracking

transformers.

tested in several applications.

It has a performance hit, but not that big.

Rough notes @ ruby nation: One man lighitng talkss

Your commit messages suck

Bad commits can lead to a lot of cost.

Good:
Describe what the change it, with a reason.

Template:

Summary [Details]
optional

Summary:
Verb, fragment, ticket# answering why

Added
fixed
upped
removed
switched
improved
updated
renamed

Vendor everything
vendor/gems

Check in your .rvmrc to communicate the context to other people.
bundle install --path vendor

echo ls vendor/ruby >> .gitignore

Packaging your gems in vendor/cache

use alias

CoffeeScript
A language that compiles into javascript.
It runs in the V8 engine.

significant white spaces

functions
square = (x ) -> function body

objects created as yaml

it has pattern matching, which look like Python bindings
Arrays and object comprehension are similar to python

Fat arrow => defines and binds a function

bundle package

Using mongo db
Relational model cannot address getting all of the data at once for an event. Why doing that? Due to performance issues.

Instead of a complex document, it was created as a few classes that had pointers.

Mongo can be hard to design upfront, but brings a huge gain later.

HTML Caching
http://diveintohtml5.org/

Cache manifest list
* all of the things that should be cached.
* All of the things that shouldn't be call

Fallback session
It is confusing. An url pattern.

Expiring the cache.

Rough notes @ ruby nation: HTML 5, Past and Pressent

These are rough notes and thoughts during the presentation. Grammar and spelling will be worse than usual.

Main point:

You can start using html 5 >>now<<

What is it? a spec, yet many people have different interpretations. According to Jason Harwig, the presenter, it is the collection of technologies that are evolving the web. He then gives an evolution of the HTML spec starting from HTML 4. WhatWG started to create a spec for evolving HTML, and browsers started implementing the features. in 2005-2006. Working draft of html5 comes out in 2010.

The doctype for html 5:


HTML has a structure elements for web app. We get now tags such as:
header
nav
article
section
aside
footer

One can embed h1s in each of the components.

There is a video and audio tag. There is an attribute for controls. In html 5, you don't need attributes. The main point is the problem with the codex. Maybe something to do more research. You can use a default to degrade to flash.

Semantic elements

They don't do anything, but they give semantic meaning.
Details
summary
progress
meter
figure
time
figcaption
mark
time
command
datalist
output
keygen
wbr

Recommended that you style them because the tags won't do anything with them.

Tags that are gone
basefont
tt
u
big
center
frame
font
s
strike
frameset
acronym
applet
They will still be there, but not recommended to be used by developers.

Form support

attributes required



-- new tag. It has js api support.

Other tipes
email
url
coolor
tel
date
month
week
time

What is supported? Go to http://wufoo.com/html5/

Javascript

Canvas. Particle engine
getElementByClassName
Drag and drop
pushState, replaceState, onhashchange (new events for state change)
Offline application Cache. (You should have to change the way your program)
Custome data attributes

input.dataset['regex'] (not widely supported)
registerProtocolHandler/ registerContentHandler ()

Related Specs

Geolocation
Cross window messaging
Cross Origin (cross domain ajax)
Resource Sharing
WebSql( IndexedDB)
WebStorage
WebWorkers
WebSockets
WebGL ( 3D)

What to use

HTML 5 Broswer test
Modernizr you include it before the javascript, and gives you CSS and javascript was to tell you what features are available. http://www.modernizr.com/ https://github.com/Modernizr/Modernizr

CSS3

It supports animation via CSS. Compass can help here too. http://compass-style.org/

What you can do
Gradients
transitions
backgrounds
transformations
shadows
masks
columns
WebFont
SVG
HSLA
RGBA

EXtJs. 4.0

umbrUI

Slides:

http://dl.dropbox.com/u/7953598/Sliderrr/index.html

Rough notes @ ruby nation: Building Semantic CSS with Compass and SASS

These are rough notes from the presentations.

CSS and markup should describe the semantic content. This will make it rank higher on searches, and makes it easier for developers know what the CSS is for.

Compass uses Sass, which is an extension of CSS, to author stylesheets. Compass will create a stylesheet folder and generate styleshets in what appears to be based on the models.

I wonder if we could use this in .net via iron ruby. I should really look into iron ruby. There are a lot of neat technologies in ruby.

Compass and Sass has a stylesheet converter to translate regular stylesheets to Sass. It looks like in Sass one can create variables, unless variables are possible in CSS and I am not aware of it. Ah, this is in CSS. Pretty cool. I didn't know: http://disruptive-innovations.com/zoo/cssvariables/ I checked Sass. The variables shown in the presentation seems to be specific to Sass.

With Compass and Sass, if the stylesheets are not balanced, it gives you an error.

Compass:
http://compass-style.org/

Sass
http://sass-lang.com/

There will be a screencast in http://www.codesherpas.com/

Cygwin and Ruby Windows

cygwin logo

I installed ruby on windows using the one-click installer. Later on, I installed cygwin as my git shell (in my experience, cygwin is the best git shell there is for windows). As most people who work with cygwin know, you can't run gem or rspec from cygwin without further configurations. It returns an error if you attempt to do that. I had to run rspec using the cmd.exe windows shell, and then commit the work using git via the cygwin shell. Not a terrible burden, but cumbersome.

I knew that I could download a cygwin copy of ruby, but I didn't want to do it because I already had my ruby gems the way I liked them. And I also noticed that running ruby -v in cygwin would give me the version of ruby found in C:\ruby192. After examining the error message that I got when I tried to run gem or rspec in cygwin, I saw that I could run gem.bat or rspect.bat successfully in the cygwin shell. So there had to be an easy way to use windows ruby and cygwin.

Well, there is.

After looking around on the net, I found this answer by Robert Wahler in stack overflow.

You go to .bashrc in your cygwin installation (found in C:\cygwin\home\ in my computer ), and at the bottom you add the following aliases:

alias gem='gem.bat'
alias rake='rake.bat'
alias erb='erb.bat'
alias irb='irb.bat'
alias rdoc='rdoc.bat'
alias ri='ri.bat'
alias rspec='rspec.bat'
alias cucumber='cucumber.bat'
alias bundle='bundle.bat'

Start the cygwin shell again, and now you will be able to use gem and rspec without having to use the .bat extensions.

http://stackoverflow.com/questions/3831131/rubygems-cygwin-posix-path-no...

Ruby script to delete .SVN folder

I needed to erase SVN folders from a directory. Ironically, I needed to do this so that I could put them back, in a cleaner format, into the repository.

Here is the script that I wrote in ruby to do this:

#! usr/bin/env ruby
 
def delete_dir(dir_name)
 d = Dir.new(dir_name)
 d.each{ |file|
   if !(file == "." || file == "..") then
     if File.directory?(file) then
       delete_dir(file)
     else
        File.delete(file)
     end  
   end
 }
 Dir.delete(dir_name)
end
 
 
def clean_svn(dir_name)
d = Dir.new(dir_name)
d.each {|file_name|
if !(file_name == "." || file_name == "..") then
  if file_name == ".svn" then
    delete_dir(file_name)
  end
  if File.directory?(file_name) then
    clean_svn(file)
  end
end
}
end
 
file = ARGV[0]
clean_svn(file)

I have two comments on the script. First, there seems to be a nice FileUtils method that does delete a folder recursively. I learned about it after I finished writing this, so delete_dir is unnecessary.

Second, the limitation of this script is that it is using recursion, and that stack must be shallow, since ruby is not an optimized functional language. So if there are too many directories within directories, it will choke at some point.

But for my purposes, this worked well.

Syndicate content