You are not logged in.

#1 2006-05-08 04:36:42

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

From Bash to Ruby

Trying to learn Ruby but when developing simple apps I am spending more time searching basic Ruby commands in Internet than doing the work. In Internet Ruby documenations are either too newbie or advance and very few samples.

To get the basic Ruby what commonly used in developing, here is list of Bash syntax I would like to have an equal Ruby.

Example.

# Bash
if [ -f "file.txt" ]; then
else
if

# Ruby:
if(FileTest.exists? "file.txt")
else

List of Bash syntax:
cat file.txt
grep "hello" file.txt
sed 's/hello/hello world/g' file.txt
awk '{ print $2 }' file.txt
echo "hello" > file.txt
echo "world" >> file.txt


Markku

Offline

#2 2006-05-08 04:50:01

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: From Bash to Ruby

A good start. Quick reference..
http://www.zenspider.com/Languages/Ruby/QuickRef.html

Good examples
http://pleac.sourceforge.net/pleac_ruby/index.html

Ruby Language docs.
http://www.ruby-doc.org/core/
http://www.ruby-doc.org/stdlib/

I will do one for ya. wink

Bash: cat file.txt
Ruby:

puts IO.read("testfile") 

"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2006-05-08 05:53:52

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: From Bash to Ruby

# grep "hello" file.txt
puts IO.read('file.txt').grep(/hello/)

# sed 's/hello/hello world/g' file.txt
puts IO.read('file.txt').gsub('hello','hello world')

# awk '{ print $2 }' file.txt
puts IO.readlines('file.txt').map{ |r| r.split[1] }

# echo "hello" > file.txt
File.open('file.txt','w'){ |f| f.puts "hello" }

# echo "world" >> file.txt
File.open('file.txt','a'){ |f| f.puts "world" }

Offline

#4 2006-05-08 16:58:25

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: From Bash to Ruby

Thanks guys for the input.
I am setting up a Ruby and Glade page.
http://user-contributions.org/wikis/use … y-Libglade

For your information this what I am working on:
http://user-contributions.org/projects/ … kginfo.jpg


Markku

Offline

#5 2006-05-13 20:09:50

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: From Bash to Ruby

jochen wrote:
# grep "hello" file.txt
puts IO.read('file.txt').grep(/hello/)

How to grep a variable? In ".grep(/hello/)" cannot use a variable.
# grep $var file.txt


Markku

Offline

#6 2006-05-14 19:05:03

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: From Bash to Ruby

Thanks.
This what I got from an other forum.

puts IO.read('file.txt').grep(/#{$var}/

Setting up a Bash to Ruby page:
http://user-contributions.org/wikis/use … sh-to-Ruby


Markku

Offline

Board footer

Powered by FluxBB