Showing posts with label install. Show all posts
Showing posts with label install. Show all posts

2008-09-06

class Array

A handful of methods that could be added to the class
Array
:
class Array

def sum
s=0
each{|e| s+=e}
s
end

def mul
m=1
each{|e| m*=e}
m
end

def mean
sum.to_f/length
end

def map_with_index
i=-1
map{|e| yield(e,i+=1)}
end

def map_with_index!
i=-1
map!{|e| yield(e,i+=1)}
end

def any_with_index?
each_with_index{|e,i| return true if yield(e,i)}
false
end

def all_with_index?
each_with_index{|e,i| return false unless yield(e,i)}
true
end

def find_index
each_with_index{|v,i| return i if yield(v)}
end

def find_indices
ret=[]
each_with_index{|v,i| ret<<i if yield(v)}
ret
end

def select_by_index(*indices)
ret=[]
indices.each{|ind| ret<<self[ind]}
ret
end

alias find_indexes find_indices

def to_hash
raise "Cannot convert to Hash!" unless all?\
{ |e|
e.respond_to? :length and e.length==2 and e.respond_to? :[]
}
h={}
each{|e| h[e[0]]=e[1]}
h
end

def keys_to_hash
h={}
each{|e| h[e]=yield(e)}
h
end

def keys_with_index_to_hash
h={}
each_with_index{|e,i| h[e]=yield(e,i)}
h
end

def with(a2)
ensure_same_length(a2)
map_with_index{|e,i| [e,a2[i]]}
end

def with_to_hash(a2)
ensure_same_length(a2)
h={}
each_with_index{|e,i| h[e]=a2[i]}
h
end

def count_all
h={}
each\
{ |e|
h[e]||=0
h[e]+=1
}
h
end

def group_by
h={}
each\
{ |e|
g=yield(e)
h[g]||=[]
h[g]<< e
}
h.map{|g,ee| ee}
end

alias contain? include?
alias has? include?

def rand
a=to_a
a[Kernel.rand(a.length)] unless a.empty?
end

private

def ensure_same_length(arg)
raise ArgumentError,"Argument must be of the same length!"\
unless arg.respond_to? :length and length==arg.length\
and arg.respond_to? :[]
end

end
They are not perfect, but I use them quite a lot.

Some of them (or similar methods) will be present in Ruby 1.9. For example there will be a method
inject
(or
reduce
) working like this:
[1,4,5].reduce(:*)           #=> 20 # 1*4*5
["a","b","dd"].reduce(:+) #=> "abdd"
As you see, they are better than my
sum
, because they work for any type for which the operation is defined. This reduce is not hard to implement, too, but it will probably work a bit faster when included in Ruby core.

Ruby 1.9 is also going to have
group_by
, working exactly like mine, as far as I know.

Move to Enumerable
One more enhancement that can be done in the above code is to move all the methods in the module
Enumerable
(just write
module Enumerable
instead of
class Array
at the top). It allows you to use these methods also with other enumerable types, like
Hash
. You'll have to test the methods, though, as not all of them make sense when used with structures where the elements are not ordered.

Add to load path
If you create some files that you'd like to be easily accessible in your Ruby programs, you can add the path to your files to Ruby load path, so that you will be able to
require
your files without giving the full path. Under Windows, just go to environment variables, and add
RUBYLIB = P:/ath/To/Your/Dir
The path will be automatically added to Ruby load path each time Ruby starts, which can be verified by typing
$:
(or
$LOAD_PATH
) in irb and looking for your path.

If you want some of your files to be loaded even without the need to
require
them, then you can add them to the environment variable RUBYOPT. This variable can already contain -rubygems. If you want the file P:/ath/To/Your/Dir/start.rb to be loaded at startup, change the variable to
-rubygems -rstart
Each word starting with -r makes ruby load a file named by the rest of the word. Ruby will find your file because you already added file path to Ruby load path. If you want to load more files at startup, it is best to
require
them from within your first file.

As you might have guessed, there is file named ubygems that the original content of the variable caused to load. The strange name is in fact chosen only to make the whole command sound reasonable. All it does is load rubygems.rb, which initialises the Gems engine, enabling programs to use additional libraries.

2008-08-10

Let's start

OK, all blog options are set up like they should, page layout is chosen, colours are adjusted. Probably it's time to write the first post here. As you might have guessed, this blog is going to be about Ruby - an interesting, inspiring, very easy and very complicated at the same time, programming language. That sentence should make it as an introduction, and let's close this topic for now.

If you are a beginner, then that's the best place for you to start. I'll try to gather some interesting beginner tutorials and useful info, and also some of my knowledge. I will also post on where to download and how to install Ruby and some useful modules. I hope that will help you a bit at the beginning.

If you are an expert... well, I'm and expert too. I guess if you are here, then your knowledge lacks some parts, as usually experts' knowledge does, not excluding my own. If what I know fits what you don't, I'll be glad to have helped you. And I'm sure I can, here or there. The problem for now is that I have to start off with the whole bunch of beginner things, and then I will get back to what's really fascinating about Ruby.

Tutorials
So let's tackle some real problem. How to start with Ruby? First, please try it out, if you haven't done it yet: A Hands-on Ruby Tutorial! It's really cool to start with it - you don't have to install anything, and you have access to the real programming and not some "now imagine that we do this and get this." Start with typing 'help' and then just follow the tutorial.

You might also want to try this (non-interactive) tutorial about "object-orientedness" of Ruby: Tutorial.

Get a console of your own
Once you see (I'm sure you do!) that there's something in it, I have great news for you! You can have a console just like the one on the webpage, only better, right on your computer! Do you think I'm using Window's calc anymore? Believe me, I don't. So: if you are using Windows, go here and download the last stable release (at the time of writing it is ruby186-26.exe). Install it, but choose carefully the path! I suggest installing it in something like C:\Ruby\Ruby\, and that's because you might want to store your programs in C:\Ruby\Programs\, your gems (about these later on) in C:\Ruby\Gems\, etc. But as you prefer. But remember, never put your own files in the installation directory. They might not survive a Ruby version upgrade. During the installation, always agree to the default options, they are reasonable.

Once you finished the installation, let's get to the Ruby console I promised you. Open the Windows console (Start | Run, cmd, Enter) and type irb, Enter. If this appears: irb(main):001:0> then here you are, the console is ready! If an error occurs instead, you will have to add the Ruby binaries path to your environment path variable. Right-click My computer on your desktop, Properties | Advanced | Environment variables, locate Path on the bottom list, Edit, and add to it ;C:\Ruby\Ruby\bin or something else if you installed Ruby elsewhere. Accept changes, close the console, open it again and try to run irb, it should work now.

If you want to have quick access to your irb, right-click on the desktop, New | Shortcut, and as the element to run type %windir%\system32\cmd.exe /k irb.bat and accept. (It is better to do it like this and not just link to the irb because if you happen to get an error that closes irb, you can still read the error message because the window won't close. What could cause such a terrible error that closes irb? Oh, if you must know, type
STDOUT.close
)

Now I'll leave you with your new toy. Play with it and explore. Here's a nice spec of basic Ruby objects like Arrays and Strings, have a look: Ruby Class Reference. Another tutorial: Why's... but... be careful with this one...

In the next post we will start up the rubygems which you are likely to need later. In following posts, I will present interesting classes, modules and functionalities, both in the standard Ruby distribution, and in the additional modules. That's all for today, see you!