Ruby vs Scala_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Ruby vs Scala

Ruby vs Scala

 2015/5/5 17:05:23  michael_roshen  程序员俱乐部  我要评论(0)
  • 摘要:1.no;attheendoflines2.baseoperatescala:vallist=Array(1,2,3,4)list.filter(_%2==0)list.filter{e:Int=>(e%2==0)}ruby:list=[1,2,3,4]list.select{|x|x%2==0}3.staictypesscala:importscala.collection.mutable.HashMapvarhash=newHashMap[Int,String]hash+=(1
  • 标签:Ruby
1. no ; at the end of lines

?

2. base operate

?

scala:
? val list = Array(1,2,3,4) ?
??
? list.filter( _ % 2 == 0)
? list.filter{e: Int => (e % 2 == 0)}
ruby:

? list = [1,2,3,4]
? list.select{|x| x % 2 == 0}

3. staic types

?

scala:
? ?import scala.collection.mutable.HashMap
? ?var hash =new HashMap[Int, String]
? ?hash += (1 -> "hello")
? ?hash += (3) #error: type mismatch;?

ruby:?
? ?hash = Hash.new
? ?hash = 3 #3

4. Dynamic calls

?

scala:
import scala.language.dynamics

class Animal extends Dynamic {
?def selectDynamic(name: String) {
? ?println("Animal wants to " + name)
?}

?def applyDynamic(name: String)(args: Any*) {
? ?println("Animal wants to " + name + args.mkString(","))
?}
}

scala> var a = new Animal
scala> a.say
Animal wants to say
scala> a.say("hello")
Animal wants to say hello

ruby:?
class Animal
?def method_missing name,*args
? ?if args.empty?
? ? ?puts "Animal says " + name.to_s
? ?else
? ? ?puts "Animal wants to " + name.to_s + args.join(",")
? ?end
? ?self
?end
end

2.2.1 :020 > animal = Animal.new
2.2.1 :020 > animal.qualk
Animal says qualk
2.2.1 :021 > animal.say("hello")
Animal wants to say hello

5. minxin

?

scala:
trait MyClass {
?def my_method = println("my method")
}
class IncludeTrait extends MyClass
scala> (new IncludeTrait).my_method
my method

ruby:?
module MyModule
?def my_method
? ?puts "my_method"
?end
end

class IncludeModule
?include MyModule
end

2.2.1 :032 >IncludeModule.new.my_method
my_method

6.duck type

?

scala:
class Duck{
?def quack = { println("duck quack..")}
?def walk = { println("duck walk..")}
}


class Platypus{
?def quack = { println("platypus quack..")}
?def walk = { println("platypus walk..")}
}

def ActAsADuck(a: {def quack; def walk}) = {
?a.quack
?a.walk
}

val duck ?= new Duck
val paltypus = new Platypus
ActAsADuck(duck)
ActAsADuck(platypus)


ruby:
class Duck
?def quack; end
?def walk; end
end

class Platypus
?def quack; end
?def walk; end
end

def act_as_a_duck(animal)
?animal.quack
?animal.walk
end

duck = Duck.new
platypus = Platypus.new

act_as_a_duck(duck)
act_as_a_duck(platypus)

7. unit test

?

scala:?

@Test def myTest(){
?val array = List(1,2,3)
?assert(array(0) === 1)
}

ruby:?

test "my test" do?
?array = [1,2,3]
?assert_equal 1, array.first
end

8. spec test

?

scala:

describe("Get of List"){
?it("(0) returns the first element") ?{
? ?val array = List(1,2,3)
? ?array(0) should be 1
?}
}

ruby :
describe "Get of Array" do?
it "first returns the first element" do?
? array = [1,2,3]
? array.first.should == 1
?end
end

9. web platform

?

scala:

Play是一个开源高性能Web框架,能够基于Java或Scala编写可扩展可伸缩的应用系统,它能自动刷新源码的变动从而具有快速开发产品的能力,它还是一个无态非堵塞的框架,易于横向扩展。Play!框架有如下特点:高开发效率:Java EE和Spring框架并不适合快速原型开发,Play框架参考动态语言的快速开发能力,它支持热重载所有的Java代码和模板等,让你更快地迭代。基于Netty构建非堵塞异步机制。RESTJSON支持:非常容易编写RESTful应用,非常适合支持HTTP路由。

Akka是一个用Scala编写的库,用于简化编写容错的、高可伸缩性的Java和Scala的Actor模型应用。它已经成功运用在电信行业。系统几乎不会宕机(高可用性99.9999999%一年只有31ms宕机

ruby:?
Ruby on Rails是一个用于开发数据库驱动的网络应用程序的完整框架。Rails基于MVC设计模式。从视图中的Ajax应用,到控制器中的访问请求和反馈,到封装数据库的模型,Rails为你提供一个纯Ruby的开发环境。发布网站时,你只需要一个数据库和一个网络服务器即可。

?

10. performance

?

?

?

  • 大小: 38.4 KB
  • 查看图片附件
发表评论
用户名: 匿名