Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want to learn Ruby?
#1
So I've been coding in Ruby/Learning for about 4 days now.
I'm giving out a free book that I used which helped me learn a lot.
Take advantage of this opportunity future ruby masters.

From what I've learned. Ruby is Turing-complete, which means that everything that can be written in any other Turing-complete language, can also be written in Ruby. Also, no language can be more than Turing-complete, which means that anything that can't be written in Ruby, can also not be written in any other language.
Reply
#2
Could you show us some of your work you've made in Ruby?
Reply
#3
(08-10-2012, 03:16 PM)Oqud Wrote: Could you show us some of your work you've made in Ruby?

Heres a combined source... Basically what I'm doing to make a music list. Similar to spotify. (Just for show) I will not be releasing anything until I'm more advanced.. I'm also using foobar as a project code.

Code:
a = [ &, "list", ^9 ]
a.type     »     Array
a.length     »     3
a[0]     »     9
a[1]     »     "list"
a[2]     »     ^00
a[3]     »     nil
b = Array.new
b.type     »     Array
b.length     »     0
b[0] = "second"
b[1] = "array"
b     »     ["second", "array"]


a = [ 1, 3, 5, 7, 9 ]     »     [1, 3, 5, 7, 9]
a[1] = 'bat'     »     [1, "bat", 5, 7, 9]
a[-3] = 'cat'     »     [1, "bat", "cat", 7, 9]
a[3] = [ 9, 8 ]     »     [1, "bat", "cat", [9, 8], 9]
a[6] = 99     »     [1, "bat", "cat", [9, 8], 9, nil, 99]

a = [ 1, 3, 5, 7, 9 ]     »     [1, 3, 5, 7, 9]
a[2, 2] = 'cat'     »     [1, 3, "cat", 9]
a[2, 0] = 'dog'     »     [1, 3, "dog", "cat", 9]
a[1, 1] = [ 9, 8, 7 ]     »     [1, 9, 8, 7, "dog", "cat", 9]
a[0..3] = []     »     ["dog", "cat", 9]
a[5] = 99     »     ["dog", "cat", 9, nil, nil, 99]

h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
h.length     »     3
h['dog']     »     "canine"
h['cow'] = 'bovine'
h[12]    = 'dodecine'
h['cat'] = 99
h     »     {"cow"=>"bovine", "cat"=>99, 12=>"dodecine", "donkey"=>"asinine", "dog"=>"canine"}

class SongList
  def initialize
    @songs = Array.new
  end
end

class SongList
  def append(aSong)
    @songs.push(aSong)
    self
  end
end

list = SongList.new
list.
  append(Song.new('title1', 'artist1', 1)).
  append(Song.new('title2', 'artist2', 2)).
  append(Song.new('title3', 'artist3', 3)).
  append(Song.new('title4', 'artist4', 4))
list.deleteFirst     »     Song: title1--artist1 (1)
list.deleteFirst     »     Song: title2--artist2 (2)
list.deleteLast     »     Song: title4--artist4 (4)
list.deleteLast     »     Song: title3--artist3 (3)
list.deleteLast     »     nil
class SongList
  def [](key)
    if key.kind_of?(Integer)
      return @songs[key]
    else
      for i in [email protected]
        return @songs[i] if key == @songs[i].name
      end
    end
    return nil
  end
end
class SongList
  def [](key)
    if key.kind_of?(Integer)
      result = @songs[key]
    else
      result = @songs.find { |aSong| key == aSong.name }
    end
    return result
  end
end
class SongList
  def [](key)
    return @songs[key] if key.kind_of?(Integer)
    return @songs.find { |aSong| aSong.name == key }
  end
end

def fibUpTo(max)
  i1, i2 = 1, 1        # parallel assignment
  while i1 <= max
    yield i1
    i1, i2 = i2, i1+i2
  end
end
fibUpTo(1000) { |f| print f, " " }

produces:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

class Array
  def find
    for i in 0...size
      value = self[i]
      return value if yield(value)
    end
    return nil
  end
end
[1, 3, 5, 7, 9].find {|v| v*v > 30 }

[ 1, 3, 5 ].each { |i| puts i }

produces:

1
3
5

["H", "A", "L"].collect { |x| x.succ }     »     ["I", "B", "M"]
f = File.open("testfile")
f.each do |line|
  print line
end
f.close
class Array
  def inject(n)
     each { |value| n = yield(n, value) }
     n
  end
  def sum
    inject(0) { |n, value| n + value }
  end
  def product
    inject(1) { |n, value| n * value }
  end
end
[ 1, 2, 3, 4, 5 ].sum     »     15
[ 1, 2, 3, 4, 5 ].product     »     120
class File
  def File.openAndProcess(*args)
    f = File.open(*args)
    yield f
    f.close()
  end
end


File.openAndProcess("testfile", "r") do |aFile|
  print while aFile.gets
end
class File
  def File.myOpen(*args)
    aFile = File.new(*args)
    # If there's a block, pass in the file and close
    # the file when it returns
    if block_given?
      yield aFile
      aFile.close
      aFile = nil
    end
    return aFile
  end
end
.create



[Image: 15com0o.gif]

[Image: 5vqbsj.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)